第一次尝试并有点卡在这个...
我有一个 ViewController 我希望在其视图中显示一个 SKScene 我已经尝试了所有我能想到的但它没有显示
- 添加 SpriteKit/SpriteKit.h
- Main.storyboard 有一个单一的视图控制器(myViewController)。
- myViewController 的视图 - 将类更改为 SKView。(见下图)
- 查看didload
- 链接到我希望加载的 SKScene (HelloScene) #import "HelloScene.h"
//ViewController.m
#import "myViewController.h"
#import <SpriteKit/SpriteKit.h>
#import "HelloScene.h"
@interface myViewController ()
@end
@implementation myViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = (SKView *) self.view;
spriteView.showsDrawCount = YES;
spriteView.showsNodeCount = YES;
spriteView.showsFPS = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
HelloScene.h
#import <SpriteKit/SpriteKit.h>
@interface HelloScene : SKScene
@end
HelloScene.m
#import "HelloScene.h"
@interface HelloScene ()
@property BOOL contentCreated;
@end
@implementation HelloScene
- (void)didMoveToView: (SKView *) view
{
[self LoadSceneContents];
}
- (void)LoadSceneContents
{
self.backgroundColor = [SKColor blueColor];
self.scaleMode = SKSceneScaleModeAspectFit;
[self addChild: [self newHelloNode]];
}
- (SKLabelNode *)newHelloNode
{
SKLabelNode *helloNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
helloNode.text = @"Hello, World!";
helloNode.fontSize = 42;
helloNode.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
return helloNode;
}
@end
上图:选择视图控制器的视图对象并将其类更改为 SKView