简体   繁体   English

从屏幕cocos2d iPhone删除精灵?

[英]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. 我将粘贴下面的代码,但基本上sprite是在“addNewBall”方法中创建的。 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? 如果球的y位置大于屏幕高度,或者x位置是否在屏幕外,您是否也想要移除球? 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. 我还应该指出,你的BallArray可能是多余的,因为无论如何你都要将所有球添加到另一个节点。 If the only children of that node are Ball s, you can get the array of balls using its children property. 如果该节点的唯一子节点是Ball ,则可以使用其children属性获取球的数组。 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.) 在这种情况下,子数组将是: self.children (有关详细信息,请参阅http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a5e739ecda0c314283a89ac389dfca2fa 。)

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. 如果在同一节点上有非Ball子节点,则可能需要添加中间节点以简化设计,以便可以使用少一个数组。

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. 你说你正在从数组中删除对象,但你没有提到你也从父CCNode中删除了精灵。

Check the methods from CCNode to remove childs: http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a0d4e615f688458c74001acf10f0ae011 检查来自CCNode的方法以删除孩子: 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. 这将从其父CCNode中删除球,并将删除所有操作和回调。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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