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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…