0

開始ライフ値を変更してコードをテストしましたが、問題は、ステートメントが有効になったときにそれらが削除されないことです。これを修正するにはどうすればよいですか? .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
4

2 に答える 2

0

あなたは命の価値をどこで評価していますか?ティックメソッドでは?

    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]]];
    }
于 2013-09-16T11:50:56.220 に答える
0

新しいモンスターを追加するたびに、前のスプライトに重ねて、Life1、Life2、Life3 の新しいスプライト セットを追加します。おそらく、ライフ ハートの 1 つのセットが必要です。

.h で

CCSprite *Life1,*Life2,*Life3;

in.m、init メソッド

    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 コードと命名規則について読んでください。それはあなたを助け、ここであなたを助けようとしている人々にとってあなたのコード サンプルをより口当たりの良いものにするでしょう。

于 2013-09-16T12:36:09.537 に答える