I have made a game in sprite kit in which balls shoot up from the bottom. I had made different Xcode Projects for each scene and then I compiled all of them into one. They do get compiled without any errors but the scenes do not run as expected.
In the different projects, infinite balls come up from the bottom, but when I run the compiled project, only one ball comes. I call addBall
in didMoveToView
.
-(void)addBall {
int randomNumber = arc4random_uniform(5);
if (randomNumber == 0) {
[self addRedBall];
SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:durationTimeMED];
[redBall runAction:moveBall];
[self performSelector:@selector(addBall) withObject:nil afterDelay:t];
}
else if (randomNumber == 1) {
[self addBlueBall];
SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:durationTimeMED];
[blueBall runAction:moveBall];
[self performSelector:@selector(addBall) withObject:nil afterDelay:t];
}
else if (randomNumber == 2) {
[self addGreenBall];
SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:durationTimeMED];
[greenBall runAction:moveBall];
[self performSelector:@selector(addBall) withObject:nil afterDelay:t];
}
else if (randomNumber == 3) {
[self addCyanBall];
SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:durationTimeMED];
[cyanBall runAction:moveBall];
[self performSelector:@selector(addBall) withObject:nil afterDelay:t];
}
else if (randomNumber == 4) {
[self addYellowBall];
SKAction *moveBall = [SKAction moveToY:CGRectGetMaxY(self.frame) duration:durationTimeMED];
[yellowBall runAction:moveBall];
[self performSelector:@selector(addBall) withObject:nil afterDelay:t];
}
}
Isn't the performSelector
is supposed to run infinitely many times?
Is there something that I am doing wrong? Or is there something that I am missing?
UPDATE: I am declaring the variables just after the @implementation and before any method. I am declaring the balls (all the colors) like this in the same area:
SKSpriteNode *yellowBall;
and then setting all their respective properties in their respective methods.
t is NSTimeInterval type variable which I declared like this in the same area:
NSTimeInterval t;