Search code examples
ioscocos2d-iphonescene

How do I send the score over to another scene?


I need some help sending the player's score when they lost over to another scene but can't figure out how.

This is the code I have right now:

if(CGRectIntersectsRect(playerOne.boundingbox, object.boundingbox))
{

    [self gameOver];
};

-(void)gameOver
{
[[CCDirector sharedDirector] replaceScene:[GameOver scene] withTransition:[CCTransistion transitionFadeWithDuration:1]
}

So basicly when these two's bounding box collide, the game is over and send you over to the gameOver scene.

How can I send over the score over to the Game Over scene too? Thanks!


Solution

  • Add a property into the GameOver class named score. Then, before you replace the scene, set the score property to whatever the score is.

    Example (note: this is untested so some types might be wrong as I've never used Cocos2d):

    -(void)gameOver {
    
        NSInteger score = /* get the score here */;
    
        GameOver *scene = [GameOver scene];
        [scene setScore:score];
        [[CCDirector sharedDirector] replaceScene:scene withTransition:[CCTransistion transitionFadeWithDuration:1]];
    
    }
    

    Now, you can just fetch the score in the GameOver scene like so:

    -(void)someMethod {
    
        NSInteger score = [self score];
        NSLog("Score: %d",score);
    
    }