xcode 从屏幕 cocos2d iphone 中删除精灵?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5175091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:22:07  来源:igfitidea点击:

Remove sprite from screen cocos2d iphone?

iphoneobjective-cxcodecocos2d-iphone

提问by Tanner Ewing

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!

我有一个我写的游戏。我准备说它完成了,但我发现了一个错误。基本上,随着您玩的时间越长,游戏会变得越慢。我的猜测是这是由于仍在屏幕外绘制的精灵。我将粘贴下面的代码,但基本上精灵是在“addNewBall”方法中创建的。在这种方法中,它被添加到一个计算其运动的数组中。在球到达离开屏幕的位置后,它会从阵列中移除,这导致它停止移动,但它仍然被“拉”​​出屏幕。如何删除精灵以便处理器不再计算它。在此先感谢您的帮助!

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

回答by Mitch Lindgren

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:

您的移除方法中似乎缺少某些条件。如果它的 y 位置大于屏幕高度,或者它的 x 位置在屏幕外,您是否也不想移除球?无论如何,在您从数组中移除球的同一个位置,您应该添加:

[self removeChild:Ball cleanup: YES]

I should also point out that your BallArrayis probably redundant, since you're adding all the balls to another node anyway. If the only children of that node are Balls, you can get the array of balls using its childrenproperty. 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#a5e739ecda0c314283a89ac389dfca2fafor more info.)

我还应该指出,您BallArray可能是多余的,因为无论如何您都将所有球添加到另一个节点。如果该节点的唯一子节点是Balls,则可以使用其children属性获取球数组。在这种情况下,子数组将是:(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 子节点,您可能需要添加一个中间节点来简化设计,以便您可以少使用一个数组。

回答by vfn

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 中移除球,并将移除所有动作和回调。

回答by Prabakaran

You need to specify your sprite,and you can use this following line.. [self removeChild:Ball cleanup: YES]

你需要指定你的精灵,你可以使用下面这行.. [self removeChild:Ball cleanup: YES]