在Cocos2D v3 iOS中按下时,CCButton无法正常工作

时间:2014-04-27 12:38:54

标签: ios cocos2d-iphone ccnode

我有一个游戏玩法scene,我在其中添加CCNode作为孩子。我的Game Over节点中有一个重播CCButton

按钮应该重启游戏场景。问题是,当我按下“重新启动”按钮时,它会通过这些行,但它不会执行replaceScene。按下时也不突出显示。这是我的相关代码:

我在GamePlay类(.m)中添加Game Node的代码:

CCNode GameOver = [[GameOverNode alloc] init];

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

[self addChild:GameOver z:5]; 

这里是GameOver Class(.h​​)的代码:

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

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

问题是,它打印出“按下按钮”,也会查看该方法的其余代码,但没有任何反应。

如果你能让我知道我做错了什么,我将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:3)

它不起作用,因为你暂停了CCDirector。删除以下行:

[[CCDirector sharedDirector] pause];

或者,如果您确实需要,请在尝试更换场景之前恢复导演。

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