1

我想将这个游戏结束场景添加到我试图为我的家庭作业做的这个游戏中,我似乎没有它,如果你杀死目标,游戏结束场景就会弹出。我试着把我的代码放在每一行,看看它是否最终能工作,但没有。所以现在我不得不寻求一些帮助。

. 文件

  - (void)addTarget10 {
 Boss *target10 = nil;    
 if ((arc4random() % 2) == 0) {{
    target10 = [WeakAndFastBoss9 boss9];
     }}  else {
    target10 = [WeakAndFastBoss9 boss9];
    }                      
 [[SimpleAudioEngine sharedEngine] playEffect:@"lastboss.mp3"];        
 // Determine where to spawn the target along the Y axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minY = target10.contentSize.height/2;
int maxY = winSize.height - target10.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target10.position = ccp(winSize.width + (target10.contentSize.width/2), actualY);
[self addChild:target10 ];
// Determine speed of the target
int minDuration = target10.minMoveDuration;
int maxDuration = target10.maxMoveDuration;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
       id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-       target10.contentSize.width/2, actualY)];
      id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                         selector:@selector(spriteMoveFinished9:)];
[target10 runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

target10.tag = 1;
[_targets addObject:target10];   
}
 -(void)gameLogicboss9:(ccTime)dt {


[self unschedule:_cmd];    



[self addTarget10];
  }

    - (void)updateboss9:(ccTime)dt {
CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                   projectile.position.y - (projectile.contentSize.height/2), 
                                   projectile.contentSize.width, 
                                   projectile.contentSize.height);

BOOL bossHit = FALSE;
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];


for (CCSprite *target1 in _targets) {
    CGRect target1Rect = CGRectMake(target1.position.x - (target1.contentSize.width/2), 
                                    target1.position.y - (target1.contentSize.height/2), 
                                    target1.contentSize.width, 
                                    target1.contentSize.height);


    if (CGRectIntersectsRect(projectileRect, target1Rect)) {

        [targetsToDelete addObject:target1];    
        bossHit = TRUE;
       Boss *boss = (Boss *)target1;
       boss.hp--;
       if (boss.hp <= 0  ) {
            [targetsToDelete addObject:target1];
            }
        break;

    }                       
}

for (CCSprite *target in targetsToDelete) {
    [_targets removeObject:target];

    [self removeChild:target cleanup:YES];                                  


     _projectilesDestroyed++;
    if (_projectilesDestroyed > 2) {


    }
}

if (bossHit) {
    //[projectilesToDelete addObject:projectile];

}
  [targetsToDelete release];



 }

  -(void)spriteMoveFinishedboss9:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];




if (sprite.tag == 1) { // target
    [_targets removeObject:sprite];





} else if (sprite.tag == 2) { // projectile





    [_projectiles removeObject:sprite];
    }     }

这是我想在目标 10/boss 9 被杀死时添加的游戏结束场景

GameOverScene *gameOverScene = [GameOverScene node];
[gameOverScene.layer.label setString:@"You Lose"];
[[CCDirector sharedDirector] replaceScene:gameOverScene];    

现在我的另一个游戏结束场景是当精灵移动通过屏幕时。如果您需要我回答任何问题,请随时提问。

4

2 回答 2

0

对于场景替换,您可以尝试将其用于您的游戏代码..

首先将此代码添加到您的 GameOverScene 类

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    GameOverScene *layer = [GameOverScene node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

让你的 GameOverClass 是 CCLayer 的子类,而不是当你想改变场景时这样做

[[CCDirector sharedDirector] replaceScene:[GameOverScene scene]];
于 2012-07-14T09:19:04.873 回答
0

好的,所以你必须创建一个新场景。这可以使用 File->New File 简单地完成,并使其成为 NSObject 的子类。然后将子类更改为 CCLayer。作为测试,您可以从 hello world 层复制代码。接下来,只需在 helloworld 层类中导入新类并创建它的实例。然后在一个方法中使用 [[CCDirector sharedDirector] replaceScene:sceneName];

您可以使用此站点获取更多信息,它非常有帮助,只需通读一遍,您就会找到答案:http://www.raywenderlich.com/352/how-to-make-a-simple-iphone-game- with-cocos2d-tutorial

于 2012-07-12T21:55:45.817 回答