Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

ios - How do I add and animate infinite number of objects coming from the bottom of the screen in random order in gradually increasing speeds?

I have 3 different sprites. I want to continuously animate these three different sprites entering from the bottom of the screen, going up increasing velocities as the number of objects coming in increases. Also, the three should come in random order.

So, I tried the following code to get any of the three sprites to come in any random order but it didn't work as expected:

int randomNumber;

for (int i = 1; i<500; i++) {
    randomNumber = arc4random_uniform(3);
    NSLog(@"Value of random Number %d" ,randomNumber);

if (randomNumber == 0) {
        [self addRedBall];
        SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:5];

    dispatch_queue_t actionDispatchRed;

    actionDispatchRed = dispatch_queue_create("com.redball.dispatch", NULL);

    dispatch_async(actionDispatchRed, ^ {   [redBall runAction:moveBall];   });
    [NSThread sleepForTimeInterval:2];   //Time interval to wait before the next ball comes in.

    }

else if (randomNumber == 1) {
        [self addBlueBall];

        dispatch_queue_t actionDispatchBlue;

        SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:5];
        actionDispatchBlue = dispatch_queue_create("com.blueball.dispatch", NULL);

        dispatch_async(actionDispatchBlue, ^{  [blueBall runAction:moveBall];  });
        [NSThread sleepForTimeInterval:2];


    }
else if (randomNumber == 2) {
        [self addGreenBall];
        SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:5
                              ];
        dispatch_queue_t actionDispatchGreen;
        actionDispatchGreen = dispatch_queue_create("com.greenball.dispatch", NULL);

        dispatch_async(actionDispatchGreen,
                       ^ {  [greenBall runAction:moveBall]; } );

    [NSThread sleepForTimeInterval:2];

    }


}

When I use the [NSThread sleepForTimeInterval:2]; , the program does not go any further than the starting screen.

When I try to run the program without the sleepForTimeInterval, a huge number of sprites come in and overlap each other resulting in only one visible sprite.

Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would not recommend using [NSThread sleepForTimeInterval:2];

Instead trying using [self performSelector:@selector(addBall) withObject:nil afterDelay:2];

-(void)addBall
{
    self.addedBall++;
    if (self.addedBall > 500)
    {
        return;
    }

    randomNumber = arc4random_uniform(3);

    if (randomNumber == 0) {
        [self addRedBall];
        SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:5];

        dispatch_queue_t actionDispatchRed;

        actionDispatchRed = dispatch_queue_create("com.redball.dispatch", NULL);

        dispatch_async(actionDispatchRed, ^ {   [redBall runAction:moveBall];   });

        [self performSelector:@selector(addBall) withObject:nil afterDelay:2];
    }
    //other else if stuff

}

Also I am unsure why you are running your SKActions in a dispatch_queue and is likely not needed.

I hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...