也许有人可以指出我正确的方向,在这里,因为我一直在用头撞墙。主要问题让我对 Sprite Kit 和 UIKit 互操作性感到困惑。
我的游戏从一个表格视图开始,它将所有玩家的游戏保存在单独的单元格中(类似于 Friends 游戏)。当一个单元被点击时,我会加载一个 SKView,它会显示一个 SKScene,其中包含预先从 Parse.com 下载的所有相关游戏数据。
问题是,我无法弄清楚如何让场景“更新”,因为没有更好的术语,包含所有当前的游戏数据。正如预期的那样,呈现的场景只显示了背景图像和其他一些图像,但应该在屏幕上的精灵却没有。相反,当我最后一次滑出 SKScene 时,屏幕上出现了精灵。我可以将所有传入的游戏数据记录到控制台中,所以我知道那里没有问题。此外,当我对场景的精灵执行某种动作(移除、刷新等)时,它会导致场景及其所有精灵“唤醒”,并且所有应该存在的精灵都会出现.
第二个可能相关的问题是,当我从 SKScene 滑回主表视图时, willMoveFromView: 不会被调用。(这是我为精灵、背景、按钮等执行所有清理代码的地方。)只有当我点击同一个表格视图单元格并返回到同一个游戏场景时才会调用 willMoveFromView:。刷出 SKScene/SKView 时不应该调用它吗?但是,我又遇到了同样的“更新”问题,场景有点被旧的数据/精灵冻结了。有任何想法吗?我已尝试包含所有相关代码,但我认为您不会发现任何异常。我的感觉是这更像是一个概念问题,我上面的评论就足够了:
@interface MGGameMenuViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, NSURLConnectionDataDelegate, UIAlertViewDelegate>
@property (nonatomic, retain) UITableView *tView;
@property (nonatomic, readwrite) MGSpriteKitViewController *skvc;
@end
@implementation MGGameMenuViewController
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
self.matchtoBePassedIn = [self.yourTurnArray objectAtIndex:indexPath.row];
[self launchGamePlaySceneWithMatchData:self.matchtoBePassedIn];
}
else if (indexPath.section == 1) {
self.matchtoBePassedIn.cardsDealt = [NSMutableArray arrayWithCapacity:9];
self.matchtoBePassedIn = [self.theirTurnArray objectAtIndex:indexPath.row];
[self launchGamePlaySceneWithMatchData:self.matchtoBePassedIn];
}
else {
NSLog(@"section chosen was neither 0 nor 1.");
}
}
// launch gameplay
-(void)launchGamePlaySceneWithMatchData:(MGMatch *)match {
if (self.skvc == nil) {
MGSpriteKitViewController *spriteKitVC = [[MGSpriteKitViewController alloc] initWithNibName:@"MGSpriteKitViewController" bundle:nil];
spriteKitVC.matchToBePassedFromGameMenuToGameplayScene = match;
self.skvc = spriteKitVC;
}
if (self.skvc) {
@try {
if (![self.skvc isBeingPresented]) {
self.skvc.matchToBePassedFromGameMenuToGameplayScene = match;
[self.navigationController pushViewController:self.skvc animated:YES];
}
}
@catch (NSException *exception) {
NSRange range = [exception.reason rangeOfString:@"Pushing the same view controller instance more than once is not supported"];
NSRange range2 = [exception.reason rangeOfString:@"Tried to pop to a view controller that doesn't exist"];
if([exception.name isEqualToString:@"NSInvalidArgumentException"] &&
range.location != NSNotFound) {
NSLog(@"[MGGameMenuViewController] NSInvalidArgumentException caught.");
if (![self.skvc isBeingPresented]) {
self.skvc.matchToBePassedFromGameMenuToGameplayScene = match;
[self.navigationController popToViewController:self.skvc animated:YES];
}
}
if ([exception.name isEqualToString:@"NSInternalInconsistencyException"] && range2.location != NSNotFound) {
if (![self.skvc isBeingPresented]) {
self.skvc.matchToBePassedFromGameMenuToGameplayScene = match;
[self.navigationController pushViewController:self.skvc animated:YES];
}
}
}
@finally {
NSLog(@"[MGGameMenuViewController] finally");
}
}
[self.navigationController.navigationBar setHidden:YES];
}
// SKView
SKView *spriteView;
@interface MGSpriteKitViewController : UIViewController
@property (nonatomic, retain) MGMatch *matchToBePassedFromGameMenuToGameplayScene;
@property (nonatomic, retain) MGGameplayScene *gameplayScene;
@end
@implementation MGSpriteKitViewController
@synthesize matchToBePassedFromGameMenuToGameplayScene, gameplayScene;
-(void)viewDidLoad {
[super viewDidLoad];
spriteView = (SKView *)self.view;
spriteView.showsDrawCount = NO;
spriteView.showsFPS = NO;
spriteView.showsNodeCount = YES;
}
-(void)viewWillAppear:(BOOL)animated {
self.gameplayScene = [[MGGameplayScene alloc] initWithSize:CGSizeMake(320.0f, 568.0f)];
self.gameplayScene.passedInMatch = self.matchToBePassedFromGameMenuToGameplayScene;
self.gameplayScene.playerImageCache = self.playerImageCache;
[spriteView presentScene:self.gameplayScene];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:YES];
self.gameplayScene = nil;
}
@end
// 游戏发生的 SKScene
@interface MGGameplayScene : SKScene
@property (nonatomic) contentCreated;
... various assets
@end
@implementation MGGameplayScene
-(void)didMoveToView:(SKView *)view {
if (self.contentCreated == NO) {
[self createSceneContents];
self.contentCreated = YES;
}
}
-(void)willMoveFromView:(SKView *)view {
[self removeAllChildren];
}
// for practicies
-(void)createSceneContents {
self.backgroundColor = [UIColor whiteColor];
self.scaleMode = SKSceneScaleModeAspectFit;
[self setAllAssetsToNil];
[self recreateAssetsWithRelevantData];
}
@end