简单的Sprite Kit场景设置出错了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的Sprite Kit场景设置出错了相关的知识,希望对你有一定的参考价值。

我正在尝试设置一个简单的Sprite Kit设置。我正在做的就是从'空应用程序'重新创建默认的xCode模板'Sprite Kit Game'。

它一直在skView.showsFPS = YES上崩溃;线。哪个我无法解释。你能?谢谢!

一些代码:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  MenuController *menuController = [[MenuController alloc] init];
  self.window.rootViewController = menuController;
  [self.window makeKeyAndVisible];
  return YES;
}

MenuController.m

- (void)viewDidLoad
{
  [super viewDidLoad];

  SKView *skView = (SKView *)self.view;
  skView.showsFPS = YES;
  skView.showsNodeCount = YES;

  MultiplayerView *gameView = [MultiplayerView sceneWithSize:skView.bounds.size];
  gameView.scaleMode = SKSceneScaleModeAspectFill;

  [skView presentScene:gameView];
}

当我启动它时,会发生以下错误:

2013-11-10 13:08:01.605 ByS[9419:70b] -[UIView setShowsFPS:]: unrecognized selector sent to instance 0x10c00bc60
2013-11-10 13:08:01.608 ByS[9419:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setShowsFPS:]: unrecognized selector sent to instance 0x10c00bc60'
答案

在界面构建器中,将窗口视图的类更改为SKView。除非先前通过铸造UIView获得SKView,否则将UIView类型铸造到SKView将不会做任何事情。

另一答案

从单视图或空应用程序开始,与通常的SpriteKit模板相比,我遇到了同样的问题。当我添加一些我发现覆盖loadView方法的代码时,我工作了。尝试将它放在ViewDidLoad上方的View Controller中。

- (void)loadView {
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    SKView *skView = [[SKView alloc] initWithFrame:applicationFrame];
    self.view = skView;
}

.h文件是通常的UIViewController。记住在链接框架后#import!:

@interface CharacterViewController : UIViewController

所以完整的.m代码将是这样的:

@implementation CharacterViewController

- (void)loadView {
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    SKView *skView = [[SKView alloc] initWithFrame:applicationFrame];
    self.view = skView;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"CharacterViewController viewDidLoad");
    // Do any additional setup after loading the view.

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;


    // Create and configure the scene.
    CharacterScene *character = [[CharacterScene alloc]initWithSize:CGSizeMake(skView.bounds.size.width, skView.bounds.size.height)];

    character.backgroundColor = [UIColor blueColor];

    [skView presentScene:character];

}
另一答案

或者你可以在prepareForSegue:或任何其他演示之前快速修复它

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     UIViewController *destinationVC = segue.destinationViewController;
     SKView * skView = (SKView *)destinationVC.view;

    if (![skView isKindOfClass:[SKView class]]) {
        skView = [[SKView alloc] initWithFrame: skView.frame]; // self.view.bounds
        destinationVC.view = skView;
    }
}
另一答案

在MenuController.m中替换您的初始化

SKView *skView = (SKView *) self.view;

SKView *skView = [[SKView alloc initWithFrame : self.view.frame];
  • 您只需要通过赋予self.view框架来初始化SKView的对象,而不是通过指定self.view的引用对象。

以上是关于简单的Sprite Kit场景设置出错了的主要内容,如果未能解决你的问题,请参考以下文章

Sprite Kit 最后得分数据

Sprite-kit 主要角色选择器

Sprite Kit 场景编辑器 GameScene.sks 场景宽度和高度

Sprite Kit SKShapeNode 创建两个节点而不是一个

在 Sprite Kit 场景中隐藏 iAd

Sprite Kit bodyAtPoint 和 bodyInRect 返回不正确的值?