Xcode 6/SpriteKit/Swift 中的场景大小 [英] Scene size in Xcode 6 / SpriteKit / Swift

查看:35
本文介绍了Xcode 6/SpriteKit/Swift 中的场景大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直被困在 Xcode 中做一些应该非常简单的事情,我正在尝试向场景中添加一个精灵并让它位于左下角:

I've been getting stuck doing what should be really simple stuff in Xcode, I am trying to add a sprite to the scene and for it to sit in the bottom left corner:

var groundTexture : SKTexture = SKTexture(imageNamed: "dirt-block")    
var ground : SKSpriteNode = SKSpriteNode(texture: groundTexture)
ground.position = CGPointMake(ground.size.width/2, ground.size.height/2)
self.addChild(ground)

这没有显示任何东西,所以我查看了场景尺寸,self.frame.size.width 和 self.frame.size.height 返回的宽度为 1024,高度为 768,无论如何方向.

This doesn't show anything, so I took a look at what the scene dimensions are and self.frame.size.width and self.frame.size.height are returning a width of 1024 and a height of 768, regardless of orientation.

如果这对答案有任何影响,我希望在横向模式下修复此问题?

I want this fixed in landscape mode if that makes any difference to the answer?

我显然缺少一个与设置场景有关的非常基本的想法,如果有人能提供任何线索,我将不胜感激.

I'm obviously missing a very fundamental idea to do with setting up the scene, if anybody could shed any light it would be much appreciated.

谢谢.

推荐答案

您必须编辑 GameViewController:

You have to edit the GameViewController:

viewDidLoad() 中删除所有内容,除了 super.viewDidLoad()

Cut everything from viewDidLoad() except super.viewDidLoad()

覆盖viewWillLayoutSubviews

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        var skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.size = skView.bounds.size
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}

您还应该设置场景大小,如您所见

Also you should set the scene size as you can see

scene.size = skView.bounds.size

希望能帮到你

还有一个很棒的教程,可以更好地解释这一点:http://www.raywenderlich.com/49721/how-to-使用 spritekit 创建突破游戏(第 1 章或第 2 章)

There is also a great Tutorial where this is explained better: http://www.raywenderlich.com/49721/how-to-create-a-breakout-game-using-spritekit (Chapter 1 or 2)

这篇关于Xcode 6/SpriteKit/Swift 中的场景大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆