Remove sprite from cocos2d iphone screen?

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

}


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


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

[BallArray_ removeObject: Ball];
}
+3
source share
3

, . , y , x ? , , , :

[self removeChild:Ball cleanup: YES]

, BallArray, , , node. node Ball s, , children. : self.children (. http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a5e739ecda0c314283a89ac389dfca2fa .)

node, node, , .

+8
+3

You need to specify your sprite, and you can use the following line. [self removeChild: Clean the ball: YES]

+1
source

Source: https://habr.com/ru/post/1795900/


All Articles