2012-07-12 106 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