1

ゲームの一時停止画面を作成しようとしています。ゲームは、独立して (左から右に) 移動する障害物を使用して、背景を (上から下に) 連続的にスクロールすることで開発されます。

以下はコードです:

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 は単なるノード クラスです。

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と、一時停止と再開が完全に機能します。しかし、これらのいずれかを実装すると、quitGame の時点で、replaceSceneどういうわけか機能しません。これは、ゲームが一時停止モードになるためだと思いますが、5 ~ 10 秒の遅延でスケジューラを使用して replaceScene を呼び出しても機能しません。

stopAnimationこれらとを使用しないとpause、与えられたスプライトの一部CCActionが一時停止されず、ゲームが一時停止されても続行されます。

私は Cocos2d 3.0 を使用しており、適切なヘルプが見つからないため、ご協力をお願いします。

4

1 に答える 1

0

ヒントをくれた @LearnCocos2D に感謝します。

コードstopAnimationまたはstartAnimationおよびpauseおよびresumeメソッドを削除するだけで、一時停止/再開を正常に実装できます。pausedこれらすべてをノードのプロパティに置き換えました。

ゲームを一時停止するには、ゲーム内のすべてのノードに対して paused = YES を設定し、その逆で再開する必要があります。

于 2015-01-16T10:50:41.707 に答える