设置SKSpriteNode位置会使精灵消失

时间:2014-06-18 22:08:28

标签: ios sprite-kit swift

在过去几天阅读了有关如何使用SpriteKit定位精灵的文档后,我似乎无法做出正面或反面。 图像本身是181×88。我的

import SpriteKit

class PlayScene: SKScene {

let runningBar = SKSpriteNode(imageNamed:"runningbar")

override func didMoveToView(view: SKView) {
    self.backgroundColor = UIColor(hex:0x80D9FF)
    self.runningBar.position = CGPointZero
    self.addChild(self.runningBar)
}...

我的按钮根本没有出现。我认为,因为左下角是0,0,我会看到屏幕左下角按钮的右上角。文档说它将纹理集中在按钮上。

如果我在那里放了1344×64的另一张图像,那么我会在左下角看到图像右上角的一点点。我对坐标系的工作原理感到困惑。我读到的是锚点在精灵的中心。

目前我有scene.scaleMode = .AspectFill

1 个答案:

答案 0 :(得分:0)

我现在也在学习。访问变量runningBar时,您不需要使用“self”。访问它。此外,我更改了您的位置代码,使其基于当前帧的计算。现在它被设置为直接中心,但你应该能够弄清楚如何移动它,只要记住CGPointMake需要:(x,y)。

import SpriteKit

class PlayScene: SKScene {

let runningBar = SKSpriteNode(imageNamed:"runningbar")

override func didMoveToView(view: SKView) {
    self.backgroundColor = UIColor(hex:0x80D9FF)

    runningBar.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5)
    self.addChild(runningBar)
}
相关问题