我正在尝试为我的游戏创建暂停屏幕。游戏是在连续滚动背景(从上到下)与独立移动(从左到右)的障碍物的情况下开发的。
下面是代码:
暂停场景.h
@interface PauseScene : CCNode {}
@property (nonatomic, retain) HelloWorldScene *mainScene;
//+ (PauseScene *)scene;
- (id) init;
@end
暂停场景.m
@implementation PauseScene
- (id) init {
if (self = [super init]) {
// Adding RESUME and QUIT button and few others to make the whole Pause screen
}
return self;
}
// Called the method when Resume button is selected
- (void) Resume:(id) sender {
//[[CCDirector sharedDirector] startAnimation];
//[[CCDirector sharedDirector] resume];
self.mainScene.userInteractionEnabled = YES;
[self.mainScene removeChild: self];
[self.mainScene StartGame]; // StartGame in MainScene contains the same code which commented out above, I will paste StartGame method later
}
// Quit method is called when the Quit button is pressed
- (void) Quit:(id) sender {
//[[CCDirector sharedDirector] resume];
//[[CCDirector sharedDirector] startAnimation];
self.mainScene.userInteractionEnabled = YES;
[self.mainScene removeChild: self];
NSLog(@"Pre Reload", nil);
//[self.mainScene scheduleOnce:@selector(ReloadGame) delay: 1.0f];
[self.mainScene ReloadGame]; // Reload method contains the code commented above
}
@end
PauseScene 仅仅是节点类。
MainScene 文件中的 StartGame 和 ReloadGame 方法
- (void) StartGame {
// Create and display HUD layer, like, scores, navigation buttons, etc.
// Resuming the game
//[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
}
- (void) ReloadGame {
// Resume the game
//[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
[[CCDirector sharedDirector] replaceScene:[HelloWorldScene scene] withTransition:[CCTransition transitionFadeWithDuration:1.0f]];
}
暂停按钮显示在 MainScene 的 HUD 层,所以,当点击暂停按钮时,我们会暂停游戏并显示 PauseScene
- (void)onPauseClicked:(id)sender {
// Code to display the pause node of this scene
// Pause the game
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];
}
当我注释掉它stopAnimation
并分开时pause
,暂停和恢复工作完美。但是当我实现其中任何一个时,在退出游戏的时候,replaceScene
它并没有以某种方式工作。我认为这是因为游戏进入了暂停模式,但即使我使用调度程序在 5-10 秒的延迟上调用 replaceScene,它也无法正常工作。
如果我不使用这些stopAnimation
and ,即使我的游戏暂停pause
,一些给出的精灵也不会被暂停并继续。CCAction
感谢您的帮助,因为我正在使用 Cocos2d 3.0 并且无法找到任何适当的帮助。