在SKScene中定位SKSpriteNode的问题

时间:2014-11-25 11:41:14

标签: swift sprite-kit

这是我第一个使用SpriteKit的项目,我正在关注THIS教程 但当我尝试使用此代码在 20:10 进入该教程时,将该位置赋予Image:

playScene.swift

import SpriteKit

class playScene : SKScene {

let runningBar = SKSpriteNode(imageNamed: "bar")

override func didMoveToView(view: SKView) {
    println("We are at the new scene!")
    self.backgroundColor = UIColor(hex: 0x80D9FF, alpha: 1)
    self.runningBar.anchorPoint = CGPointMake(0.5, 0.5)
    self.runningBar.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - (self.runningBar.size.height / 2))
    self.addChild(self.runningBar)

}
override func update(currentTime: NSTimeInterval) {

    }
}

我想在屏幕底部给出位置,但我得到了这个输出:

enter image description here

但输出应该是:

enter image description here

GameScene.swift (如果需要)

import SpriteKit

class GameScene: SKScene {

let playButton = SKSpriteNode(imageNamed: "play")

override func didMoveToView(view: SKView) {

    self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    self.addChild(self.playButton)
    self.backgroundColor = UIColor(hex: 0x80D9FF, alpha: 1)

}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches{
        let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.playButton{
            var scene = playScene(size: size.self)
            let skView = self.view as SKView!
            skView.ignoresSiblingOrder = true
            scene.scaleMode = .ResizeFill
            scene.size = skView.bounds.size
            skView.presentScene(scene)
        }

    }

}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    }
}

有人能告诉我怎样才能实现这个目标?

先谢谢。

1 个答案:

答案 0 :(得分:2)

您需要正确查看教程。您已将以下行设置错误:

self.runningBar.anchorPoint = CGPointMake(0.5, 0.5)
self.runningBar.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - (self.runningBar.size.height / 2))

他们应该是:

self.runningBar.anchorPoint = CGPointMake(0, 0.5)
self.runningBar.position = CGPointMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame) + (self.runningBar.size.height / 2))

为了更好地理解坐标系,请查看documentation