SKAction: How to Animate Random Repeated Actions

和自甴很熟 提交于 2019-12-05 22:12:58

No bells or whistles, but I think this should get you on your way:

Edit: Sorry it should be:

SKAction *randomXMovement = [SKAction runBlock:^(void){
    NSInteger xMovement = arc4random() % 20;
    NSInteger leftOrRight = arc4random() % 2;
    if (leftOrRight == 1) {
        xMovement *= -1;
    }
    SKAction *moveX = [SKAction moveByX:xMovement y:0 duration:1.0];
    [aSprite runAction:moveX];
}];

SKAction *wait = [SKAction waitForDuration:1.0];
SKAction *sequence = [SKAction sequence:@[randomXMovement, wait]];
SKAction *repeat = [SKAction repeatActionForever:sequence];
[aSprite runAction: repeat];

Yep there's a great Action you can use.

So instead of doing your current runAction do this:

[self.bomber runAction:[SKAction repeatActionForever:sequence]];

You'll also need to change your moveAction moveToX: value to something like arc4random

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!