从节点

时间:2015-05-27 05:40:07

标签: ios swift sprite-kit

我有这个sene,里面有很少的节点。它是圈内圈内的圆圈。按下里面的最小圆圈,我用几个SKActions制作了这个动画。

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

    let growOut = SKAction.scaleTo(1.2, duration: 0.3)
    let growIn = SKAction.scaleTo(1.0, duration: 0.5)
    let glowOut = SKAction.fadeAlphaTo(0.5, duration: 0.3)
    let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
    let sOut = SKAction.group([glowOut, growOut])
    let sIn = SKAction.group([glowIn, growIn])
    let circleTouched = SKAction.sequence([sOut, sIn])
    let circleRepeat = SKAction.repeatActionForever(circleTouched)


    for touch in touches {
        let location = (touch as! UITouch).locationInNode(self)
        if let theCircle = nodeAtPoint(location) as SKNode?{
        if theCircle.name == "SmallCircle" {
            theCircle.runAction(circleRepeat, withKey: "circleTouched")


            }

    }

    }
}

触摸结束时,我会删除此操作:

   override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    let growIn = SKAction.scaleTo(1.0, duration: 0.5)
    let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
    let sIn = SKAction.group([glowIn, growIn])

    for touch in touches {
        let location = (touch as! UITouch).locationInNode(self)
        if let theCircle = nodeAtPoint(location) as SKNode?{
          theCircle.runAction(sIn)
            theCircle.removeActionForKey("circleTouched")
        }


}
}

但是当我用手指向这个圆圈移开手指时,它会一直播放。我尝试用touchesMoved函数修复它,但它对我来说有点奇怪。

覆盖func touchesMoved(touches:Set,withEvent event:UIEvent){

    let growIn = SKAction.scaleTo(1.0, duration: 0.5)
    let glowIn = SKAction.fadeAlphaTo(1, duration: 0.5)
    let sIn = SKAction.group([glowIn, growIn])
    let circleRepeat = SKAction.repeatActionForever(circleTouched)

    for touch in touches {
        let location = (touch as! UITouch).locationInNode(self)
        if let theCircle = nodeAtPoint(location) as SKNode?{
            if theCircle.name != "SmallCircle" {

                println("I'M MOVING FROM NODE!!!")
                theCircle.runAction(sIn)
                theCircle.removeActionForKey("circleTouched")

            }

        }
     }   
    }

所以我收到了这个&#34;我从节点开始#34;信号,但行动不要停止。 我哪里错了?相同的代码适用于touchesEnded函数。

1 个答案:

答案 0 :(得分:1)

因为这个问题而发生问题

if let theCircle = nodeAtPoint(location) as SKNode?

每次移动鼠标时,&#34; theCircle&#34;复位。例如,您第一次单击圆圈,&#34; theCircle&#34;是您单击的圆,因此动画附加到它。比如第二次,你点击了背景,这次&#34; theCircle&#34;是背景,所以它没有你设置的动画,因此没有办法删除动画&#34;。

解决方法是,您将圆圈声明为范围级别变量,通常位于类中,位于顶部:

var smallCircle: SKSpriteNode!

然后在didMoveToView(view:SKView)中,配置圆圈(如果使用.sks):

smallCircle = childNodeWithName("the circle name") as! SKSpriteNode
smallCircle.name = "SmallCircle"

这一次,您可以指向touchMoved中的圆圈:

for touch in (touches as! Set<UITouch>) {
  let location = touch.locationInNode(self)
  if let theCircle = nodeAtPoint(location) as SKNode?{
    if theCircle.name != "SmallCircle" {
      smallCircle.runAction(sIn)
      smallCircle.removeActionForKey("circleTouched")
    }
  }
}

最后,你会发现动画停止了。