ios - UIView > SKView ... willMoveFromView : not being called when expected and sprites not being "updated"

标签 ios cocoa-touch ios7 sprite-kit

也许有人可以给我指出正确的方向,在这里,因为我一直在用头撞墙。让我全神贯注于 Sprite Kit 和 UIKit 互操作性的主要问题。

我的游戏从一个表格 View 开始,它将所有玩家的游戏保存在单独的单元格中(与 friend 游戏一样)。当点击一个单元格时,我会加载一个 SKView,它会显示一个 SKScene,其中包含预先从 Parse.com 下载的所有相关游戏数据。

问题是,由于没有更好的术语,我不知道如何让场景“更新”所有当前的游戏数据。正如预期的那样,呈现的场景仅显示背景图像和一些其他图像,但屏幕上应该显示的 Sprite 却没有。相反,它是我最后一次从 SKScene 中滑出时出现在屏幕上的 Sprite 。我可以将所有传入的游戏数据记录到控制台中,所以我知道那里没有问题。此外,当我对场景的 sprite 执行某种操作(删除、刷新等)时,它会导致场景及其所有 sprite 有点“醒来”,并且所有应该在那里的 sprite 都会出现.

第二个可能相关的问题是,当我从 SKScene 滑动回主 TableView 时,不会调用 willMoveFromView:。 (这是我为 Sprite 、背景、按钮等执行所有清理代码的地方)只有当我点击同一个表格 View 单元格并返回到同一个游戏场景时, willMoveFromView: 才会被调用。滑出 SKScene/SKView 时不应该调用它吗?但是,我再次遇到了同样的“更新”问题,即场景有点被旧数据/ Sprite 卡住了。有任何想法吗?我已尝试包含所有相关代码,但我认为您不会发现任何异常之处。我的感觉是这更像是一个概念性问题,我上面的评论就足够了:

@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

最佳答案

在 Storyboard 中将 MGSpriteKitViewController 中的 uiview 的类指定为 skview。

关于ios - UIView > SKView ... willMoveFromView : not being called when expected and sprites not being "updated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19011759/

相关文章:

ios - 展平任意数组

ios - 轮播不起作用

ios - UIRefreshControl 与两个异步 json 下载来填充 UITableView 行和自定义标题 View

objective-c - NSURLConnections 属于我的模型还是 Controller ?

iphone - 如何在 UITextView 内添加内联标记

iphone - UINavigationBar 标题和右键

ios - 我已经创建了 UIButton 方法。现在我想为操作按钮设置角半径。我怎样才能添加这个?

ios - spritekit 游戏中的音效开/关

iOS itunesconnect 沙箱测试账号(无效年份)

ios7 - SKSpriteNode 在不应该弹跳的时候弹跳