SKScene是什么?
当进入一个游戏之后,出现在屏幕上的内容叫做场景,每一个游戏都是有若干的场景组成的,在SpriteKit游戏框架中的场景就是SKScene对象组成,SKScene类似于iOS中的UIView对象.下面就说一下SKScene关于的技术点.
如何使用SKScene
- 1.新建一个NewScene,继承自SKScene,你会发现在NewScene.m文件中什么都没有,这里我们要重写他的初始化方法
-(instancetype)initWithSize:(CGSize)size
在这个方法里,我们给我添加一个背景颜色
- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor redColor];
}
return self;
}
- 1.1. 除了可以添加背景颜色外,还可以添加背景图片,这就需要用到
SKSpriteNode
// 根据一张图片创建一个node 并作为场景的子node
SKSpriteNode *node1 = [SKSpriteNode spriteNodeWithImageNamed:@"znb 2.jpg"];
node1.size = self.view.bounds.size;
node1.position = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));
[self addChild:node1];
- 2.现在有一个场景了,在原先项目中已经有一个MyScene ,现在我们就用这两个scene做转场动画
我们在MyScene.m 和NewScene.m的-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
中进行场景的切换,这时候,我们需要使用presentScene:(nonnull SKScene *) transition:(nonnull SKTransition *)
这个方法调用过渡动画,我们看一下都有哪些过渡动画(SKTransition类方法),因为过渡效果太多,过渡效果需要自行演示..
+ (SKTransition *)crossFadeWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)fadeWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)fadeWithColor:(SKColor *)color duration:(NSTimeInterval)sec;
+ (SKTransition *)flipHorizontalWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)flipVerticalWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)revealWithDirection:(SKTransitionDirection)direction duration:(NSTimeInterval)sec;
+ (SKTransition *)moveInWithDirection:(SKTransitionDirection)direction duration:(NSTimeInterval)sec;
+ (SKTransition *)pushWithDirection:(SKTransitionDirection)direction duration:(NSTimeInterval)sec;
+ (SKTransition *)doorsOpenHorizontalWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)doorsOpenVerticalWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)doorsCloseHorizontalWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)doorsCloseVerticalWithDuration:(NSTimeInterval)sec;
+ (SKTransition *)doorwayWithDuration:(NSTimeInterval)sec;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
MyScene *scene = [[MyScene alloc] initWithSize:self.size];
[self.view presentScene:scene transition:[SKTransition doorwayWithDuration:1]];
}