简体   繁体   中英

Remove sprite from screen cocos2d iphone?

I have a game that I wrote. I am about ready to call it finished but I found a bug. Basically the game gets slower as the longer you play. My guess is this is due to sprites that are still being drawn off screen. I will paste the code below but basically the sprite is created in the "addNewBall" method. In this method it is added to an array which calculates its motion. After the ball reaches a position where it is off the screen it is removed from the array which causes it to stop moving but it is still being "drawn" off screen. How do I remove the sprite so the processor no longer calculates it. Thanks in advance for your help!

Tanner

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];

}


//And here is where it is removed...


if (Ball.position.y <= -100) {

[BallArray_ removeObject: Ball];
}

You seem to be missing some conditions in your removal method. Don't you also want to remove the ball if its y position is greater than the screen height, or if its x position is off-screen? At any rate, in the same place that you're removing the ball from the array, you should add:

[self removeChild:Ball cleanup: YES]

I should also point out that your BallArray is probably redundant, since you're adding all the balls to another node anyway. If the only children of that node are Ball s, you can get the array of balls using its children property. In this case, the child array would be: self.children (See http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a5e739ecda0c314283a89ac389dfca2fa for more info.)

If you have non-Ball children on the same node, you might want to add an intermediate node to simplify the design so that you can use one less array.

You said you are removing the objects from the arrays, but you didn't mention that you are also removing the sprite from the parent CCNode.

Check the methods from CCNode to remove childs: http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a0d4e615f688458c74001acf10f0ae011

You could use:

[Ball removeFromParentAndCleanup:YES];

This will remove the ball from it's parent CCNode and will remove all actions and callbacks.

您需要指定您的精灵,并且您可以使用以下行.. [self removeChild:Ball cleanup:YES]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM