1

2 つの UIViewController があります。ユーザーはUIViewControllerから始めます。次に、ユーザーはボタンをクリックしてゲームをプレイします。したがって、アプリケーションは、SKViewでゲームプレイを表示する別のビューコントローラーであるPlayViewControllerに順次切り替えます。ActionSceneは、ゲーム プレイを担当する SKScene サブクラスですユーザーが失敗すると、アプリケーションはラベル (SKLabelNode) を表示し、ゲームを終了するかどうかを尋ねます。ユーザーがこのラベルをタップすると、ゲーマーは PlayViewController を介してメイン ビュー コントローラー (UIViewController) に戻されます。したがって、ActionScene はこのタスクを PlayViewController に委任する必要があると考えています。以下は ActionScene からのものです。

// ActionScene.h
#import <SpriteKit/SpriteKit.h>

@protocol actionDelegate;
@interface ActionScene : SKScene

@property (weak) id <actionDelegate> delegate;
@end

@protocol actionDelegate <NSObject>
@required

// Delegate
- (void)closeScene;
@end

// ActionScene.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
    SKNode *n = [self nodeAtPoint:[touch locationInNode:self]];
        if (n != self && [n.name isEqual: @"doneLabel"]) { // It was else if before.  I've made a change.
            [[self childNodeWithName:@"doneLabel"] removeFromParent]; // The user chooses to end the game by tapping doneLabel (SKLabelNode)
            [self.delegate closeScene];
        }
    }
}

PlayViewController に関しては、次のように ActionScene を SKView に提示し、ユーザーを UIViewController に送り返す役割も果たします。

// PlayViewController.h
#import "ActionScene.h"

@interface PlayViewController : UIViewController <actionDelegate>
@property (strong,nonatomic) ActionScene *actionScene;

@end

// PlayViewController.m
- (void)viewWillLayoutSubviews {
    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:spriteView];

    SKScene *scene = [[ActionScene alloc] initWithSize:spriteView.frame.size];
    [self.actionScene setDelegate:self]; // Letting ActionScene delegate a task to itself
    [spriteView presentScene:scene];
}

- (void)closeScene {
    // It doesn't get a call from ActionScene
}

そのため、ユーザーが完了ラベルをタップすると、closeScene が作動するように設計されています。しかし、このデリゲート メソッドが呼び出されることはありません。過去 9 か月間、iOS アプリケーションを開発していません。しかし、私は正しい考えを持っていると思います。私は何を間違っていますか?これまでのところ、このトピックこのトピック、その他を読みました。

ご協力ありがとうございました。

// 追加説明 //

  1. ユーザーが doneLabel をタップする前に、すべてのアクションが削除されます。
  2. ゲームアプリがtouchBeganに入ります。ただし、[spriteView presentScene:scene] 行はスキップします。
4

2 に答える 2

0

この投稿を参考に、私の必死の策が功を奏しました。次の変更が PlayViewController.m に加えられ、ユーザーを UIViewController に戻します。

- (void)viewWillLayoutSubviews {
    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:spriteView];

    SKScene *scene = [[ActionScene alloc] initWithSize:spriteView.frame.size];
    [self.actionScene setDelegate:self]; // Letting ActionScene delegate a task to itself
    [spriteView presentScene:scene];
}

- (void)viewWillLayoutSubviews {        
    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:spriteView];

    ActionScene *scene = [ActionScene sceneWithSize:spriteView.bounds.size];
    [scene setDelegate:self];
    [spriteView presentScene:scene];
}

これで、大きな心配をすることなく、フル ペースでゲーム開発を進めることができます。

于 2014-02-27T00:02:23.883 に答える