在场景中添加游戏

时间:2012-07-12 20:24:12

标签: iphone objective-c xcode ios5 cocos2d-iphone

我想在场景中添加这个游戏,我正在尝试为我的作业做这个游戏,我似乎没有它,如果你杀死目标,游戏场景会弹出。我尝试将我的代码放在每一行,看看它是否会最终起作用但是没有。所以现在我需要一些帮助。

。 m档

  - (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];    

现在我的另一场比赛就是当精灵被移动通过屏幕。如果你需要我回答任何问题,请随时问。

2 个答案:

答案 0 :(得分:0)

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

您可以使用此网站获取更多信息,非常有用,只需仔细阅读即可找到答案:http://www.raywenderlich.com/352/how-to-make-a-simple-iphone - 游戏与 - cocos2d的教程

答案 1 :(得分: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]];