2013-12-13 73 views
0

我是cocos2d中的新成员。我们在创建新的精灵对象时遇到问题。它不会被移除到显示器上。当我们添加新的生命时,Sprite对象不会被删除(添加心灵精灵)。如何在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; 
    } 

//下面的代码去掉心脏(减少生命)。

- (void) addMonster:(ccTime)dt { 

     //select a random monster from the _monsters Array 
     int selectedMonster = arc4random() % [_monsters count]; 

     Monster *monster = [_monsters objectAtIndex:selectedMonster]; 
     int m = [monster movement]; 

     CCSprite *spriteMonster = [[CCSprite alloc] initWithFile:[monster monsterSprite]]; 
     spriteMonster.tag = [monster tag]; 

     CGSize winSize = [CCDirector sharedDirector].winSize; 
     int minX = spriteMonster.contentSize.width/2; 
     int maxX = winSize.width - spriteMonster.contentSize.width/2; 
     int rangeX = maxX - minX; 
     int actualY = (arc4random() % rangeX) + minX; 

     //BLOCK 2 - Determine speed of the monster 
     int minDuration = [monster minVelocity]; 
     int maxDuration = [monster maxVelocity]; 
     int rangeDuration = maxDuration - minDuration; 
     int actualDuration = (arc4random() % rangeDuration) + minDuration; 

    if(m == 1){ 

     spriteMonster.position = ccp(actualY,winSize.height + spriteMonster.contentSize.height/2); 
      [self addChild:spriteMonster]; 

      //BLOCK 4 - Create the actions 
      CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(actualY,-spriteMonster.contentSize.height/2)]; 

      CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) { 
       [_monstersOnScreen removeObject:node]; 
       [node removeFromParentAndCleanup:YES]; 

       // Remove lifes 
       lives--; 

       // [[hearthArray lastObject] removeFromParentAndCleanup:YES]; 

       [self removeChild:[hearthArray lastObject] cleanup:YES]; 
       [hearthArray removeLastObject]; 

       NSLog(@"m=1 when array : %@",hearthArray); 

       if(lives == 0) 
        [[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]]; 
      }]; 

      [spriteMonster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 

      [_monstersOnScreen addObject:spriteMonster]; 
     } 

    } 

//在下面添加新生命使用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); 
} 

请帮助我。预先感谢。

回答

0

你increaseliveswhentouch硬币的方法应该是这样..

-(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++; 
} 

您必须添加只有一个心脏的对象。有没有必要重新创建的所有对象,如果你想创建首先删除所有对象..

+0

+1 @vivek Bansal谢谢。 – Rain

相关问题