0
votes

I'm making a platform game with SpriteKit, and I'm trying to use the method setValue:forKey: inside of the touchesMoved:withEvent method because I have a mess: I need to remove an animation of type SKAction. I run this SKAction animation in touchesMoved:withEvent with an if, checking all the needed conditions. One of these conditions is this:

if(NO ==[[self childNodeWithName:@"hero"] hasActions])
{
     //code to run animation
}

I check if it has no actions, because I don't want to create more animations than needed. The animation runs forever while I touch the screen with my finger, and it will remove only if I remove my finger from the screen, in the touchesEnded:withEvent method

So until I'm touching the screen, the hero animation will loop forever. The problem I have is if I move my finger from the right side of the screen to the left side of the screen, the animation is still the same: the hero running to the right, even if the sprite is moving to the left, because the if check if there is actions, and don't create the new animation to the left, and of course don't remove the right animation.

I think I can fix it setting a key for the animation action, and removing it in the same method with an if and using the location of the finger. The problem is that I don't know how to use the removeActionForKey method. I've tried reading documentation but I didn't find it very clear. Can anyone post me some code example of how to use it?

1

1 Answers

0
votes

You need to use runAction:withKey: method:

// if direction == right
[node removeActionForKey:@"left"];
SKAction *animationRight = [SKAction animateWithTextures:......];
[node runAction:animationRight withKey:@"right"];

...........

// if direction == left
[node removeActionForKey:@"right"];
SKAction *animationLeft = [SKAction animateWithTextures:......];
[node runAction:animationLeft withKey:@"left"];