【问题标题】:Grabbing GameScene size from SKSpriteNode class从 SKSpriteNode 类中获取 GameScene 大小
【发布时间】: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


【解决方案1】:

scene.frame.size.width 更改为scene.size.widthSKScene 有一个 size 属性。它还具有view 的属性。请注意,size 以点为单位。如果您在进行数学运算以根据大小生成位置,则需要将场景的 anchorPoint 考虑在内。

默认的anchorPoint为0, 0,即视图框架矩形的左下角。这就是为什么事情“更多地在左下角”。

我假设 self.frame 起作用的原因是因为您在 SKView 类中这样做。

【讨论】:

  • 感谢您的快速信息回复!我实际上是在我的 object.class 文件中调用 touchesEnd/Begin/Moved 函数来扩展 SKSpriteNode 以避免移动我的背景精灵。它在大多数情况下都有效,除非我试图获取 SKView 的尺寸。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多