我还在玩iOS中的SpriteKit,并且已经进行了大量的阅读和大量的实验.关于坐标,框架和子节点,我发现了*我感到很困惑.
考虑这段代码,我试图在我的宇宙飞船精灵周围绘制一个绿色框用于调试目的:
func addSpaceship()
{
let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
spaceship.name = "spaceship"
// VERSION 1
spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
let debugFrame = SKShapeNode(rect: spaceship.frame)
debugFrame.strokeColor = SKColor.greenColor()
spaceship.addChild(debugFrame)
// VERSION 2
// spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
spaceship.setScale(0.50)
self.addChild(spaceship)
}
Run Code Online (Sandbox Code Playgroud)
如果我用上面标有"VERSION 1"的行添加太空船精灵的集合,我得到这个:

这显然是错的.但是,如果我评论了行上面标有"版本1",而是使用了行标"版本2",我得到我想要的东西:

请注意,标记为版本1和版本2的行的实际代码是相同的:spaceship.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
那么为什么当我设置宇宙飞船精灵的位置时这很重要?
根据我的思维方式,宇宙飞船精灵的位置与debugFrame的位置无关,因为debugFrame是宇宙飞船精灵的孩子,因此,它的坐标应该相对于宇宙飞船精灵的框架 - 对吗?
谢谢WM
*这与我昨天提出的问题有些相关:
在iOS上的SpriteKit中,缩放纹理的精灵会产生不正确的帧?
但是a)我现在明白了一个,而且b)这个是不同的,它应该得到自己的问题.
更新:
嗯 - 谢谢,伙计们对于下面的想法,但我仍然没有得到它,也许这会有所帮助.
我修改了我的代码以打印出相关的位置和框架:
func addSpaceship()
{
let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
spaceship.name = "spaceship"
println("Spaceship0 Pos \(spaceship.position) Frame = \(spaceship.frame)")
// VERSION …Run Code Online (Sandbox Code Playgroud)