문제

I'm trying to move a Sprite over the screen from left to right. The sprite should start at a random y position on the right side offscreen. With a repeatActionForever and an integer with a random Y number i want the sprite to repeat the action starting from different y Positions. Any ideas on how to achieve this besides putting the random int in an update method? Is it anyhow achieveable through actions?

I use this method on the Sprite:

    int randomY = (arc4random()%121;

    SKAction *pos = [SKAction moveTo:CGPointMake((STAGESIZE.width+(self.size.width/2)),randomY) duration:0];
    SKAction *move = [SKAction moveToX:0-self.size.width/2 duration:3];
    SKAction *wait = [SKAction waitForDuration:1 withRange:5];

    SKAction *sequence = [SKAction sequence:@[pos,move,wait]];
    SKAction *repeater = [SKAction repeatActionForever:sequence];
    [self runAction:repeater];
도움이 되었습니까?

해결책

There is no way to randomise standard actions once defined and running. But there is a workaround you can use to achieve desired effect using [customActionWithDuration].1

SKAction* randomPositionAction = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node,CGFloat elapsedTime){
                int randomY = arc4random_uniform(121);
                //Set position instead of running action with duration 0
                [node setPosition:CGPointMake((STAGESIZE.width+(self.size.width/2)),randomY)];
            }];

RandomY is randomised every time action is run and position is set according to that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top