如果Else语句有效但不更新cocos2d

时间:2013-09-16 11:45:42

标签: objective-c cocos2d-iphone 2d-games

我通过更改启动生活vaule来测试我的代码,问题是它不会删除它们,因为语句变得有效,我该如何解决这个问题?我已经尝试将它放在我的.m文件中,但它似乎无法在任何地方正常工作,任何关于它会去哪里的想法?我会发布.m但它大概是500行所以它有点大,所以我只是粘贴了它的相关位。我也是一个15岁的孩子,我对cocos2d开发还很新 守则

  - (void) addMonster {
  CCSprite * monster = [CCSprite spriteWithFile:@"startH.png"];

// Determine where to spawn the monster along the Y axis
CGSize winSize = [CCDirector sharedDirector].winSize;
int minY = monster.contentSize.height / 2;
int maxY = winSize.height - monster.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;

// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = ccp(winSize.width + monster.contentSize.width/2, actualY);
[self addChild:monster];


// Determine speed of the monster}
if (Strategyscore < 10) {
    int minDuration = 5.0;
    int maxDuration = 10.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;
    eate the actions
    CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration
                                                position:ccp(-monster.contentSize.width/2, actualY)];
    CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
        [node removeFromParentAndCleanup:YES];
        [_monsters removeObject:node];
        Life--;

        CCSprite *Life3 = [CCSprite spriteWithFile:@"heart.png"];
        Life3.position = ccp(210,200);
        CCSprite *Life2 = [CCSprite spriteWithFile:@"heart.png"];
        Life2.position = ccp(220,200);
        CCSprite *Life1 = [CCSprite spriteWithFile:@"heart.png"];
        Life1.position = ccp(230,200);
        [self addChild:Life3];
        [self addChild:Life2];
        [self addChild:Life1];
        if(Life == 2) {
            [self removeChild:Life3];
        }
        else if(Life == 1) {
            [self removeChild:Life2];
            [self removeChild:Life3];
        }
        else if(Life <= 0) {
            [self removeChild:Life1];
            [self removeChild:Life2];
            [self removeChild:Life3];


    // Cr [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
        }
    }];
    [monster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
    //collision stuff
    monster.tag = 1;
    [_monsters addObject:monster];
}

也是.h文件

int StrategyBullet;
int Strategyscore;
int high;
int Life;

CCLabelTTF *highlabel;
CCLabelTTF *StrategyBulletLabel;
CCLabelTTF *StrategyscoreLabel;
@interface Strategy: CCLayer
{
NSMutableArray * _monsters;
NSMutableArray * _projectiles;
int _monstersDestroyed;

}


+(CCScene *) scene;

@end

2 个答案:

答案 0 :(得分:0)

你在哪里贬低生命价值?在tick方法中?

    if(Life == 2) {
        [self removeChild:Life3];
    }
    else if(Life == 1) {
        [self removeChild:Life2];
        [self removeChild:Life3];
    }
    else if(Life <= 0) {
        [self removeChild:Life1];
        [self removeChild:Life2];
        [self removeChild:Life3];
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene]]];
    }

答案 1 :(得分:0)

每次添加新怪物时,都会添加一组新的精灵Life1,Life2和Life3,叠加在之前的精灵上。你可能想拥有一套生命之心。

in .h

CCSprite *Life1,*Life2,*Life3;
<。> in .m,init method

    Life3 = [CCSprite spriteWithFile:@"heart.png"];
    Life3.position = ccp(210,200);
    Life2 = [CCSprite spriteWithFile:@"heart.png"];
    Life2.position = ccp(220,200);
    Life1 = [CCSprite spriteWithFile:@"heart.png"];
    Life1.position = ccp(230,200);

    [self addChild:Life1];
    [self addChild:Life2];
    [self addChild:Life3];

并在你的actionMoveDone调用块中,不要删除它们,只是让它们不可见

CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {

    [node removeFromParentAndCleanup:YES];
    [_monsters removeObject:node];
    Life--;


    if(Life == 2) {
        Life3.visible=NO;
    }
    else if(Life == 1) {
        Life3.visible=NO;
        Life2.visible=NO;
    }
    else if(Life <= 0) {
        Life3.visible=NO;
        Life2.visible=NO;
        Life1.visible=NO;
    }
}];

首发。我只是尽可能地将它作为“喜欢你的编码风格”,但最终你会发现不同的模式,因为游戏变得更加复杂。阅读正常的iOS代码和命名约定,它将帮助您,并使您的代码示例更适合那些试图在这里帮助您的人。