在cocos2d中删除精灵

发布于 2024-12-12 02:58:30 字数 1440 浏览 0 评论 0原文

-(void) update:(ccTime *)dt{
    for( int i=0; i < [objects count]; i++){
         CCSprite *obj = (CCSprite *) [objects objectAtIndex:i];
         if(obj.position.y <= 40){
            [obj stopAllActions];
            [self removeChild:obj cleanup:YES]; /* ?? */
            [objects removeObjectAtIndex:i];
         }
    }

我希望这段代码能够删除精灵,但它不起作用。精灵仍然在屏幕上。我在这里做错了什么,请帮助。谢谢。

编辑:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vase.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"vase.png"];
[self addChild:spriteSheet z:1 tag:1];
//CCSpriteFrame must be created for each animation frame.
NSMutableArray *animFrames = [NSMutableArray array];
for (int i = 1; i <= 3; i++) {
    NSString *file = [NSString stringWithFormat:@"frame %d.png", i];
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:file];
    [animFrames addObject:frame];
}
//Sprite is positioned and then animated.
CCSprite *player = [CCSprite spriteWithSpriteFrameName:@"frame 1.png"];     
CCAnimation *anim = [CCAnimation animationWithFrames:animFrames delay:0.20f];
CCRepeat *repeat = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:YES] times:3];


[player runAction:repeat];

player.position = startPostion;
[spriteSheet addChild:player];
-(void) update:(ccTime *)dt{
    for( int i=0; i < [objects count]; i++){
         CCSprite *obj = (CCSprite *) [objects objectAtIndex:i];
         if(obj.position.y <= 40){
            [obj stopAllActions];
            [self removeChild:obj cleanup:YES]; /* ?? */
            [objects removeObjectAtIndex:i];
         }
    }

}

I am expecting this code to remove the sprites but its not working. sprite is still on the screen. what i am doing wrong here , please help. thanks.

EDIT:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vase.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"vase.png"];
[self addChild:spriteSheet z:1 tag:1];
//CCSpriteFrame must be created for each animation frame.
NSMutableArray *animFrames = [NSMutableArray array];
for (int i = 1; i <= 3; i++) {
    NSString *file = [NSString stringWithFormat:@"frame %d.png", i];
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:file];
    [animFrames addObject:frame];
}
//Sprite is positioned and then animated.
CCSprite *player = [CCSprite spriteWithSpriteFrameName:@"frame 1.png"];     
CCAnimation *anim = [CCAnimation animationWithFrames:animFrames delay:0.20f];
CCRepeat *repeat = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:YES] times:3];


[player runAction:repeat];

player.position = startPostion;
[spriteSheet addChild:player];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本宫微胖 2024-12-19 02:58:31

我们正在从 self 中删除精灵,但该精灵不是 self 的子级。它是 spriteSheet 的子元素,因为您已经编写了:

[spriteSheet addChild:player];

因此,您必须从 spriteSheet 而不是 self 中删除 sprite。但更容易:

[sprite  removeFromParentAndCleanup: YES];

Ure are removing the sprite from self, but the sprite is not a child of self. It's a child of spriteSheet because you've written:

[spriteSheet addChild:player];

So you have to remove the sprite from spriteSheet instead of self. But it's easier to:

[sprite  removeFromParentAndCleanup: YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文