0

I have a gameplay scene, over which I add a CCNode as a child. My Game Over node has a replay CCButton in it.

The button is supposed to restart the game play scene. The problem is, when I press the "Restart" button, it goes through the lines but it doesn't perfrom replaceScene. Also it doesn't highlight when pressed. Here's my relevant code:

The code where I add the Game Over Node in my GamePlay Class (.m):

CCNode GameOver = [[GameOverNode alloc] init];

[self unscheduleAllSelectors];
[self stopAllActions];
[[OALSimpleAudio sharedInstance] stopBg];
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];

[self addChild:GameOver z:5]; 

and here's the code for GameOver Class (.h):

@interface GameOverNode:CCNode {
 CCButton *_aButton;
}
@property (nonatomic, retain) CCButton *aButton;
- (id)init;
- (void)ButtonPressed:(id)sender;

and Game Over (.m):

-(id)init {
if ( self = [super init] ){

    CCSpriteFrame *replayFrame = [CCSpriteFrame frameWithImageNamed:@"Replay.png"];


    _aButton = [CCButton buttonWithTitle:@"" spriteFrame:replayFrame];
    _aButton.position = ccp(200,200);
    [_aButton setTarget:self selector:@selector(ButtonPressed:)];
    [self addChild:_aButton z:2];
}
return self;
}

- (void)ButtonPressed:(id)sender
{
NSLog(@"Button pressed");
CCTransition* t = [CCTransition transitionFadeWithDuration:0.4f];
t.outgoingSceneAnimated = YES;
t.incomingSceneAnimated = YES;

[[CCDirector sharedDirector] replaceScene:[GamePlayScene scene] withTransition:t];
}

The thing is, it prints out "Button pressed", also goes through the rest of the code of the method, but nothing happens.

I'll appreciate if you can let me know what I am doing wrong.

Thanks!

Hyder
  • 1,163
  • 2
  • 13
  • 37

1 Answers1

3

It does not work because you have paused the CCDirector. Remove the following line:

[[CCDirector sharedDirector] pause];

Alternatively if you really need that, resume the director before you attempt to replace the scene.

[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] replaceScene:[GamePlayScene scene] withTransition:t];
lucianomarisi
  • 1,552
  • 11
  • 24
  • Your reply makes sense. I tried but no luck. I commented all this part: `[self unscheduleAllSelectors]; [self stopAllActions]; [[OALSimpleAudio sharedInstance] stopBg]; [[CCDirector sharedDirector] stopAnimation]; [[CCDirector sharedDirector] pause];`, and then ran it, surprisingly the gameplay stops itself when the GameOver Node is added. But then again, the `replaceScene` is not working. – Hyder Apr 28 '14 at 14:50
  • I don't see anything wrong with your GameOverNode from the code you have added, try putting a button on your gameplay scene and use that to push you new instance of GamePlayScene, so you confirm the problem is not with GameOverNode. (I'm assuming that your scene class method creates a new instance of your scene `+ (GamePlayScene *)scene { return [[GameplayScene alloc] init];}` – lucianomarisi Apr 29 '14 at 11:12