【发布时间】:2014-12-18 09:42:32
【问题描述】:
我在 Swift 中拖着我的 SKSpriteNode。拖动精灵后,我想将其返回到原来的位置。做这个的最好方式是什么?现在,我正试图将它直接移回其指定位置(参见代码)。但是,我无法获取场景的屏幕尺寸。使用我当前正在运行的代码,它将节点返回到比我想要的更多的左下角(我相信这是因为我引用了自我)。如何获得场景的大小?
我尝试使用scene.frame.size.width,但仅使用self.frame 成功。使用scene.frame.size.width 返回"SKScene? does not have a member 'frame'"
这是我在SKSpriteNode 类文件中的当前函数:
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
for touch in touches {
zPosition = 0
let newPosition = CGPointMake(CGRectGetMaxX(scene.frame.size.width) * 0.14 , CGRectGetMidY(scene.frame.size.height) * 0.25)
removeAllActions()
let returnSprite = SKAction.moveTo(newPosition, duration: 0.3)
let dropDown = SKAction.scaleTo(0.5, duration: 0.2) //Added Drop Effect
runAction(SKAction.group([returnCard,dropDown]))
}
}
感谢您的时间和耐心。我是新手。
更新: 我仍然不知道如何在精灵节点中调用场景。但是,我通过在 init 参数中添加一个 CGPoint 并从那里引用该位置来解决这个问题。
【问题讨论】:
-
你检查节点的锚点了吗然后在这种情况下重用它们
-
感谢您的提示!我将节点放置在 GameScene 中。我试图在我的 object.class 文件中引用来自 GameScene 的视图,该文件是 SKSpriteNode 的扩展。我没有在任何时候重新定位锚点,所以它应该在 (0, 0)。
-
谢谢。我在设置场景时使用了您的建议来存储位置。
标签: ios swift sprite-kit cgpoint