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 アプリケーションを開発していません。しかし、私は正しい考えを持っていると思います。私は何を間違っていますか?これまでのところ、このトピック、このトピック、その他を読みました。
ご協力ありがとうございました。
// 追加説明 //
- ユーザーが doneLabel をタップする前に、すべてのアクションが削除されます。
- ゲームアプリがtouchBeganに入ります。ただし、[spriteView presentScene:scene] 行はスキップします。