Ios 在cocos2d中创建触摸时,如何删除精灵对象?

Ios 在cocos2d中创建触摸时,如何删除精灵对象?,ios,cocos2d-iphone,sprite,Ios,Cocos2d Iphone,Sprite,我是cocos2d的新手。我在创建新的精灵对象时遇到问题。它不会被移到显示屏上。添加新生命时不会删除精灵对象。添加心形精灵 //在这里,我创造了一个心脏形状的生活 -(id) init { if( (self=[super init]) ) { hearthArray = [[NSMutableArray alloc] init]; lives = 4; for(NSInteger ilive = 0; ili

我是cocos2d的新手。我在创建新的精灵对象时遇到问题。它不会被移到显示屏上。添加新生命时不会删除精灵对象。添加心形精灵

//在这里,我创造了一个心脏形状的生活

    -(id) init {
        if( (self=[super init]) ) {

     hearthArray = [[NSMutableArray alloc] init];
            lives = 4;

            for(NSInteger ilive = 0; ilive<lives; ilive++){
                CCSprite *hearth = [CCSprite spriteWithFile:@"hearth.png"];
                hearth.position = ccp( ((ilive+1)*50), winSize.height - 50);
                [hearthArray insertObject:hearth atIndex:ilive];
                [self addChild:hearth];
            }

           return self;
    }
//当触摸特定对象时,使用for loop.when添加新生命

-(void)increaseLivesWhentouchCoin{

      NSLog(@"lives is get when add live : %i",lives);

    NSLog(@"hearthArray when toch coin: %@",hearthArray);

     lives = lives+1;
    NSLog(@"lives+1 : %i",lives);

    for(NSInteger i = 0; i<lives; i++){
        hearth = [CCSprite spriteWithFile:@"hearth.png"];
        CGSize winSize = [CCDirector sharedDirector].winSize;
        hearth.position = ccp( ((i+1)*50), winSize.height-50);
        [hearthArray insertObject:hearth atIndex:i];
        [self addChild:hearth];
    }
    NSLog(@"hearthArray out for loop: %@",hearthArray);
}

请帮帮我。提前谢谢。

当触摸硬币法应该是这样的时候,你的寿命会增加

-(void)increaseLivesWhentouchCoin{
    CCSprite *hearth = [CCSprite spriteWithFile:@"hearth.png"];
    CGSize winSize = [CCDirector sharedDirector].winSize;
    hearth.position = ccp( ((lives+1)*50), winSize.height-50);
    [hearthArray insertObject:hearth atIndex:lives];
    [self addChild:hearth];
    lives++;
}
您只需添加一个心脏对象。如果要先创建并删除所有对象,则无需再次创建所有对象

-(void)increaseLivesWhentouchCoin{
    CCSprite *hearth = [CCSprite spriteWithFile:@"hearth.png"];
    CGSize winSize = [CCDirector sharedDirector].winSize;
    hearth.position = ccp( ((lives+1)*50), winSize.height-50);
    [hearthArray insertObject:hearth atIndex:lives];
    [self addChild:hearth];
    lives++;
}