简体   繁体   中英

CCButton not working completely when pressed in Cocos2D v3 iOS

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM