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.