I'd like to run a repeating SKAction
but with random values on each repeat. I have read this question here that shows one way to do this. However, I want my sprite's movements to be animated, not simply changing its position. One solution I have come up with is to run a sequence of actions, with the final action calling my move method in a recursive fashion:
- (void)moveTheBomber {
__weak typeof(self) weakSelf = self;
float randomX = // determine new "randomX" position
SKAction *moveAction = [SKAction moveToX:randomX duration:0.25f];
SKAction *waitAction = [SKAction waitForDuration:0.15 withRange:0.4];
SKAction *completionAction = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node, CGFloat elapsedTime) {
[weakSelf moveTheBomber];
}];
SKAction *sequence = [SKAction sequence:@[moveAction, waitAction, completionAction]];
[self.bomber runAction:sequence];
}
Recursively calling this method feels 'icky' to me, but given my limited experience with SpriteKit, seemed like the most obvious way to accomplish this.
How can I animate the random movement of a sprite on screen forever?