我们在cocos2d中创建触摸时如何删除sprite对象?

时间:2013-12-13 06:18:30

标签: ios cocos2d-iphone sprite

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

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

    -(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循环添加新的生命。当触摸特定对象时。

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

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:0)

你的增值钱币硬币方法应该是这样的..

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

您只需添加一个心脏对象。如果您想先创建删除所有对象,则无需再次创建所有对象。