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

牧云@^-^@ 提交于 2019-12-13 09:55:35

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/28155526/how-do-i-add-and-animate-infinite-number-of-objects-coming-from-the-bottom-of-th

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