【问题标题】:CCButton not working completely when pressed in Cocos2D v3 iOS在 Cocos2D v3 iOS 中按下时 CCButton 无法完全工作
【发布时间】:2014-04-27 12:38:53
【问题描述】:

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

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

我在 GamePlay 类 (.m) 中添加 Game Over 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 类 (.h) 的代码:

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

游戏结束 (.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];
}

问题是,它打印出“按下按钮”,还遍历了该方法的其余代码,但没有任何反应。

如果你能告诉我我做错了什么,我将不胜感激。

谢谢!

【问题讨论】:

    标签: ios cocos2d-iphone ccnode


    【解决方案1】:

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

    [[CCDirector sharedDirector] pause];
    

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

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

    【讨论】:

    • 您的回复很有道理。我试过但没有运气。我评论了所有这部分:[self unscheduleAllSelectors]; [self stopAllActions]; [[OALSimpleAudio sharedInstance] stopBg]; [[CCDirector sharedDirector] stopAnimation]; [[CCDirector sharedDirector] pause];,然后运行它,令人惊讶的是,添加 GameOver 节点时游戏会自行停止。但话又说回来,replaceScene 不起作用。
    • 我没有从您添加的代码中看到您的 GameOverNode 有任何问题,尝试在您的游戏场景上放置一个按钮并使用它来推送您的新 GamePlayScene 实例,因此您确认问题是不是 GameOverNode。 (我假设您的场景类方法会创建场景的新实例+ (GamePlayScene *)scene { return [[GameplayScene alloc] init];}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多