2012-07-12 109 views
1

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

。 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 /轴套9被杀害

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

现在我的其他游戏对场景是当精灵移动通过screen.If添加的游戏你需要我回答任何问题,随时问。

+1

“我试着把我的代码放在每一行,看看它是否会最终工作”<=我认为有一个名称,但它绝对不是“编程”。如果你这样做,停下来。退后。休息一下。晚一点回来。再看看你的代码。使用调试器进行调试。了解发生了什么,试图找出它发生的原因。 FWIW,替换场景的代码似乎是正确的。它实际上是否被称为?你是否设置了断点或添加了NSLog来确认? – LearnCocos2D 2012-07-12 21:13:57

回答

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

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

您可以使用本网站获取更多信息,它非常有帮助,只需通读它,您就会发现您的答案:http://www.raywenderlich.com/352/how-to-make-a-simple- iphone游戏与 - cocos2d的教程

+0

嗯,已经做到了。对不起,我不清楚这一点。 – 2012-07-16 09:19:37