Cocos2d 实施暂停 - 游戏 scene/layer 问题
Cocos2d implementation of Pause - The Game scene/layer issue
我正在尝试为我的游戏创建暂停屏幕。该游戏是在连续滚动背景(从上到下)和独立移动(从左到右)的障碍物上开发的。
代码如下:
PauseScene.h
@interface PauseScene : CCNode {}
@property (nonatomic, retain) HelloWorldScene *mainScene;
//+ (PauseScene *)scene;
- (id) init;
@end
PauseScene.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 只是节点 class。
StartGame and ReloadGame methods in MainScene file
- (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]];
}
The pause button is displayed in HUD layer of MainScene, so, when the pause button is clicked, we will pause the game and display the 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
部分时,暂停和恢复工作完美。但是当我执行其中任何一个时,在 quitGame 时,replaceScene
无法正常工作。我认为这是因为游戏进入暂停模式,但即使我使用调度程序调用 replaceScene 延迟 5-10 秒,它也无法正常工作。
如果我不使用这些 stopAnimation
和 pause
,一些指定 CCAction
的 sprite 不会被暂停,即使我的游戏暂停也会继续。
非常感谢您的帮助,因为我使用的是 Cocos2d 3.0,无法找到任何合适的帮助。
感谢@LearnCocos2D 的提示。
我只需删除代码 stopAnimation
或 startAnimation
以及 pause
和 resume
方法即可成功实施 pause/resume。我只是将这些全部替换为节点的 paused
属性。
要暂停游戏,需要为游戏中的每个节点设置paused = YES,然后reverse才能继续。
我正在尝试为我的游戏创建暂停屏幕。该游戏是在连续滚动背景(从上到下)和独立移动(从左到右)的障碍物上开发的。
代码如下:
PauseScene.h
@interface PauseScene : CCNode {}
@property (nonatomic, retain) HelloWorldScene *mainScene;
//+ (PauseScene *)scene;
- (id) init;
@end
PauseScene.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 只是节点 class。
StartGame and ReloadGame methods in MainScene file
- (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]];
}
The pause button is displayed in HUD layer of MainScene, so, when the pause button is clicked, we will pause the game and display the 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
部分时,暂停和恢复工作完美。但是当我执行其中任何一个时,在 quitGame 时,replaceScene
无法正常工作。我认为这是因为游戏进入暂停模式,但即使我使用调度程序调用 replaceScene 延迟 5-10 秒,它也无法正常工作。
如果我不使用这些 stopAnimation
和 pause
,一些指定 CCAction
的 sprite 不会被暂停,即使我的游戏暂停也会继续。
非常感谢您的帮助,因为我使用的是 Cocos2d 3.0,无法找到任何合适的帮助。
感谢@LearnCocos2D 的提示。
我只需删除代码 stopAnimation
或 startAnimation
以及 pause
和 resume
方法即可成功实施 pause/resume。我只是将这些全部替换为节点的 paused
属性。
要暂停游戏,需要为游戏中的每个节点设置paused = YES,然后reverse才能继续。