Is it possible to end an SKAction mid-action?
Asked Answered
H

1

21

I have a subclass of SKSpriteNode (monsterNode). It automatically runs around the screen using vectors to follow the player. I am currently using the following action to make it run around:

SKAction *actionMove = [SKAction moveTo:actualDistance duration:time];
        [self runAction:actionMove completion:^ {
            _currentState = SVGMonsterStateIdle;
        }];

I am wondering if its possible to make it so the monsterNode actually STOPS running the action if it hits the boundary of the iOS device screen. I currently have SKSpriteNode boundaries on the edges of the screen, linked with a contact delegate to notify if the monster and walls make contact. However, that means nothing if I can't actually stop the monster's actionMove action from going to completion. The monster needs to stop at the boundaries of the screen. If it is not possible to stop an SKAction mid-execution, is there a roundabout way to do so?

Hypostatize answered 27/9, 2013 at 2:8 Comment(0)
T
37

Look at the SKNode.h header file - it has two functions listed:

- (void)removeActionForKey:(NSString *)key;
- (void)removeAllActions;

The latter will work: [monsterNode removeAllActions];

Taxiway answered 27/9, 2013 at 2:14 Comment(3)
Shoot. I looked in the wrong Documentation (SKAction docs). Thanks!Hypostatize
Follow question: There seems to be node way of setting the key property. Subclass?Efficient
Edit: instead of [node runAction:myaction], use [node runAction:myaction withKey:@"mykey"];.Efficient

© 2022 - 2024 — McMap. All rights reserved.