I have a game that I wrote. I am ready to call it complete, but I found a mistake. Basically the game gets slower the longer you play. I guess this is due to sprites that still exit the screen. I will paste the code below, but basically the sprite is created in the "addNewBall" method. In this method, it is added to the array, which calculates its movement. After the ball reaches the position where it is on the screen, it is removed from the array, because of which it stops moving, but it is still “pulled” from the screen. How to remove a sprite so that the processor no longer calculates it. Thanks in advance for your help!
Tanner
the code:
-(void) addNewBall {
NumberOfBalls = NumberOfBalls + 1;
int RandomXPosition = (arc4random() % 240) + 40;
NSString *BallFileString = @"OrangeBall.png";
switch (arc4random() % 5) {
case 1:
BallFileString = @"OrangeBall.png";
break;
case 2:
BallFileString = @"GreenBall.png";
break;
case 3:
BallFileString = @"YellowBall.png";
break;
case 4:
BallFileString = @"PinkBall.png";
break;
case 0:
BallFileString = @"BlueBall.png";
break;
}
Ball = [CCSprite spriteWithFile:BallFileString];
Ball.position = ccp(RandomXPosition, 520);
BallIsMoving = YES;
[self addChild:Ball z:10];
[AllObjectsArray_ addObject:Ball];
[BallArray_ addObject:Ball];
}
if (Ball.position.y <= -100) {
[BallArray_ removeObject: Ball];
}
source
share