【问题标题】:How do i detect a touch location in SpriteKit?如何在 SpriteKit 中检测触摸位置?
【发布时间】:2016-03-18 16:16:10
【问题描述】:

我正在尝试创建游戏,我希望能够检测屏幕上的触摸位置。我看过几篇关于这个的帖子,但他们都有过时的答案。 任何帮助表示赞赏。

【问题讨论】:

标签: sprite-kit location touch


【解决方案1】:

当您创建一个新项目并选择游戏(SpriteKit 游戏)时,如果您查看内部,您会看到已经显示了您如何处理触摸:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */

        for touch in touches {
            let location = touch.locationInNode(self)

            let sprite = SKSpriteNode(imageNamed:"Spaceship")

            sprite.xScale = 0.5
            sprite.yScale = 0.5
            sprite.position = location

            let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

            sprite.runAction(SKAction.repeatActionForever(action))

            self.addChild(sprite)
        }
    }

如您所见,有一个变量location 表示场景坐标系中的触摸位置。

【讨论】:

  • 谢谢,帮了大忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2013-03-23
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
  • 2014-12-07
  • 2013-06-29
相关资源
最近更新 更多