Swift SKScene shows as blank in Objective-C project

不羁岁月 提交于 2019-12-08 10:02:41

问题


I have an Objective-C based project in Xcode. I'm trying to add a SpriteKit scene as a smaller view in a view controller. The SpriteKit files are in swift though. I've added Floor1.sks and linked it with Floor1.swift.

Now I'm trying to load it into my Objective-C ViewController.m file. In my storyboard I made the view to the class SKView, and I think I properly made a bridging-header file. My code to insert it at the moment is:

GKScene *scene = [GKScene sceneWithFileNamed:@"Floor1"];
Floor1 *sceneNode = (Floor1 *)scene.rootNode;
sceneNode.scaleMode = SKSceneScaleModeAspectFit;

SKView *skView = (SKView *)_skView;

[skView presentScene:sceneNode];

skView.showsFPS = YES;
skView.showsNodeCount = YES;

The view loads when I run the app successfully, but it's just an empty view with a light grey background with the node count which is 0, and the FPS. What am I doing wrong, how come it won't load my proper view?


回答1:


I had the same problem. Forgive my english, i use google translator. I decided to look at it as written files when I create a new cross-platform game. Namely cross-platform. It does not have much other scene initialization. And it all worked for me. Used to work only on iOS 10, and now iOS 9. Screenshots I attached to them

GameViewController.m

    #import "GameViewController.h"
#import "GameScene.h"

@implementation GameViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    GameScene *scene = [GameScene newGameScene];

    // Present the scene
    SKView *skView = (SKView *)self.view;
    [skView presentScene:scene];

    skView.ignoresSiblingOrder = YES;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
}

[GameScene.m][2]

#import "GameScene.h"

@implementation GameScene {
    SKShapeNode *_spinnyNode;
    SKLabelNode *_label;
}

+ (GameScene *)newGameScene {
    // Load 'GameScene.sks' as an SKScene.
    GameScene *scene = (GameScene *)[SKScene nodeWithFileNamed:@"GameScene"];
    if (!scene) {
        NSLog(@"Failed to load GameScene.sks");
        abort();
    }

    // Set the scale mode to scale to fit the window
    scene.scaleMode = SKSceneScaleModeAspectFill;

    return scene;
}

[GameScene.h][3]

#import <SpriteKit/SpriteKit.h>

@interface GameScene : SKScene

+ (GameScene *)newGameScene;

@end


来源:https://stackoverflow.com/questions/41946374/swift-skscene-shows-as-blank-in-objective-c-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!