【问题标题】:See if SKShapeNode touched?: How to Identify node?看看 SKShapeNode 是否触及?:如何识别节点?
【发布时间】:2014-12-09 18:28:32
【问题描述】:

我正在 Swift Xcode 6 中做一个有趣的小项目。函数 thecircle() 由 didMoveToView() 中的计时器以一定的速率调用。我的问题是如何检测是否点击了显示屏上的多个圆形节点中的任何一个?我目前看不到在此函数中访问单个节点的方法。

func thecircle() {
    let circlenode = SKShapeNode(circleOfRadius: 25)
    circlenode.strokeColor = UIColor.whiteColor()
    circlenode.fillColor = UIColor.redColor()

    let initialx = CGFloat(20) 
    let initialy = CGFloat(1015)

    let initialposition = CGPoint(x: initialx, y: initialy)
    circlenode.position = initialposition

    self.addChild(circlenode)

    let action1 = SKAction.moveTo(CGPoint(x: initialx, y: -20), duration: NSTimeInterval(5)) 
    let action2 = SKAction.removeFromParent()
    circlenode.runAction(SKAction.sequence([action1, action2]))
}

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    这有很多问题。

    1. 您不应该在游戏中创建任何循环计时器。一个场景带有一个update 方法,在游戏的每一帧都会被调用。大多数情况下,您将在这里检查场景中的变化。

    2. 您无法从thecircle 方法之外访问circlenode。如果您想从其他地方访问,您需要将 circlenode 设置为场景的属性。

      例如:

      class GameScene: BaseScene {
          let circlenode = SKShapeNode(circleOfRadius: 25)
      
    3. 您需要使用方法touchesBegan。它应该与您的 spritekit 项目一起提供。您可以通过以下方式检测对节点的触摸:

      override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
      
         for touch: AnyObject in touches {
      
             // detect touch in the scene
            let location = touch.locationInNode(self)
      
            // check if circlenode has been touched
            if self.circlenode.containsPoint(location) {
                  // your code here
            }
         }
      }
      

    【讨论】:

    • 酷!把它放在我的代码中,但有一个问题。一个圆圈应该出现,但是当它要添加下一个时,它会抛出一个错误:。我尝试了circlenode.removeFromParent(),但这使得只有一个节点完成了它应该完成的任务。然后不显示其他节点。有什么想法吗?
    • 您将相同的 circlenode 添加两次。如果你想在每次触屏的时候添加一个新的circlenode,你需要在每次触屏的时候创建一个新的circlenode..
    • 那我该怎么做呢?节点数组??如果有人有一些sn-ps的代码,那就太好了!非常感谢到目前为止的帮助
    • 当他们触摸屏幕时,您只需制作一个新的。只需创建一个新的circlenode,将其放置在触摸位置并将其添加到场景中..
    • 如果“circlenode”已经存在,我不知道如何创建一个新的circlenode
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2022-01-21
    相关资源
    最近更新 更多