在cocos2d中删除精灵
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们正在从
self
中删除精灵,但该精灵不是self
的子级。它是spriteSheet
的子元素,因为您已经编写了:因此,您必须从
spriteSheet
而不是 self 中删除 sprite。但更容易:Ure are removing the sprite from
self
, but the sprite is not a child ofself
. It's a child ofspriteSheet
because you've written:So you have to remove the sprite from
spriteSheet
instead of self. But it's easier to: