SKScene에서 게임 오버 작업을 UIViewController에 위임하는 데 어려움이 있음
-
21-12-2019 - |
문제
두 개의 UIViewController가 있습니다.사용자는 다음으로 시작합니다. UIViewController.그런 다음 사용자는 버튼을 클릭하여 게임을 플레이합니다.따라서 애플리케이션은 다른 뷰 컨트롤러로 순차적으로 전환됩니다. PlayViewController, 게임 플레이를 보여줍니다. SK뷰.그리고 액션씬 이다 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] 줄은 건너뜁니다.
해결책 2
참고로 이 게시물, 나의 필사적인 조치가 성공했습니다.사용자를 UIViewController로 다시 가져오기 위해 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)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];
}
이제 큰 걱정 없이 게임 개발을 최대 속도로 진행할 수 있습니다.
다른 팁
귀하의 코드를 구현했는데 모든 것이 매력처럼 작동합니다.줄에 중단점을 설정합니다.
[self.delegate closeScene];
호출되는지 확인하세요.