Cobbler
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";< br /> 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:B all z:10];
[AllObjectsArray_ addObject:Ball];
[BallArray_ addObject:Ball];
}
/ /And here is where it is removed...
if (Ball.position.y <= -100) {
[BallArray_ removeObject: Ball] ;
}
[self removeChild:Ball cleanup: YES]
I should also point out that your BallArray may be redundant, because you have to add all balls to another node anyway. If the only child node of that node is Ball, you can use its child properties to get the ball’s Array. In this case, the sub-array will be: self.children (see http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a5e739ecda0c314283a89ac389dfca2fa for details).
If there are non-Ball child nodes on the same node, you may need to add intermediate nodes to simplify the design so that one less array can be used.
I have a game that I wrote. I am ready to say it is finished, but I found an error. Basically, as the game time goes on, the game slows down. My guess is that this is because it is still drawing on the screen Sprite. 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 that calculates its movement. After the ball reaches the position where it leaves the screen, it starts from Move out of the array, which makes it stop moving, but it is still “pulled out” of the screen. How to delete the sprite so that the processor no longer counts it. Thanks in advance for your help!
Cobbler
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";< br /> 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];
}
// And here is where it is removed...
if (Ball.position.y <= -100) {
[BallArray_ removeObject: Ball];
}
You seem to be missing some conditions in the delete method. If the y position of the ball is greater than the screen height, or if the x position is off the screen , Do you also want to remove the ball? Anyway, in the same place where you removed the ball from the array, you should add:
[self removeChild:Ball cleanup: YES]
I should also point out that your BallArray may be redundant, because you have to add all balls to another node anyway. If the only child node of that node is Ball, you can use its child properties to get the ball’s Array. In this case, the sub-array will be: self.children (see http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a5e739ecda0c314283a89ac389dfca2fa for details).
If there are non-Ball child nodes on the same node, you may need to add intermediate nodes to simplify the design so that one less array can be used.