1
votes

I have this sene with few nodes in it. It is circle inside circle inside circle. Pressing on smallest circle inside, I have this animation made with few 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")


            }

    }

    }
}

When touch ends, I remove this action like so:

   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")
        }


}
}

But when I move my finger out of this circle with actions on it, it keeps on playing. I've tried to fix it with touchesMoved function, but it acts kind of strange for me.

override 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")

            }

        }
     }   
    }

So I receive this "I'M OUT OF THE NODE" signal, but action don't stop. Where am I wrong? The same code works for touchesEnded function.

1

1 Answers

1
votes

The problem happens because of this one

if let theCircle = nodeAtPoint(location) as SKNode?

Everytime you move your mouse, "theCircle" resets. For example, for the first time, you click the circle, "theCircle" is the circle you clicked, hence the animation is attached to it. For the second time, say, you clicked the background, this time "theCircle" is the background, so it does not have the animation you set, therefore there is no way to remove "the animation".

The solution is, you declare the circle as a scope level variable, usually inside the class, at the top:

var smallCircle: SKSpriteNode!

Then in didMoveToView(view: SKView), configure the circle(if you use .sks):

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

This time, you can point to the circle in 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")
    }
  }
}

At last, you will find the animation stopped.