SKView Transtion
How to move other scene
Use presentScene
- (void)transitToScene:(SKScene *)newScene { newScene.scaleMode = SKSceneScaleModeAspectFill; // Transition new scene SKTransition *transition = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0]; [self.view presentScene:newScene transition:transition]; }
Transition Directions
- SKTransitionDirectionUp
- SKTransitionDirectionDown
- SKTransitionDirectionRight
- SKTransitionDirectionLeft
SceneController
SceneController is controller for transition.
Use NSNotification to detect transition.
SceneController.h
@interface SceneController : NSObject @property (nonatomic, weak) SKView *view; @end
SceneController.m
@implementation SceneController - (id)init { self = [super init]; if(self ) { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(receiveStart:) name:@"GameStartNotification" object:nil]; } return self; } - (void)receiveStart:(NSNotification *)notify { SKScene *scene = [NewScene sceneWithSize:self.view.bounds.size]; [self transitToScene:scene]; } - (void)transitToScene:(SKScene *)newScene { newScene.scaleMode = SKSceneScaleModeAspectFill; // Transition new scene SKTransition *transition = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0]; [self.view presentScene:newScene transition:transition]; } @end
ViewController and Scene
Let’s start example.
GameViewController.m
@interface GameViewController () @property (nonatomic)SceneController *sceneController; @end @implementation GameViewController - (void)loadView { self.view = [[SKView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; } - (void)viewDidLoad { [super viewDidLoad]; SKView * skView = (SKView *)self.view; skView.showsFPS = YES; skView.showsNodeCount = YES; skView.showsDrawCount = YES; self.sceneController = [[SceneController alloc] init]; self.sceneController.view = skView; // First scene SKScene *scene = [TitleScene sceneWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; [skView presentScene:scene]; } @end
loadView is required to use ViewController from AppDelegate.
TitleScene.h
#import <SpriteKit/SpriteKit.h> @interface TitleScene : SKScene @end
TitleScene.m
#import "TitleScene.h" @implementation TitleScene - (id)initWithSize:(CGSize)size { if(self = [super initWithSize:size]) { // background color self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0]; // title SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; myLabel.text = @"Fun!"; myLabel.fontSize = 34; myLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)); [self addChild:myLabel]; // label SKLabelNode *operation = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; operation.text = @"Tap screen to start."; operation.fontSize = 17; operation.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)-50); [self addChild:operation]; // blink action SKAction *fadeOut = [SKAction fadeOutWithDuration:1.0]; SKAction *fadeIn = [SKAction fadeInWithDuration:0.0]; SKAction *blink = [SKAction sequence:@[fadeOut, fadeIn]]; [operation runAction:[SKAction repeatActionForever:blink]]; } return self; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:@"GameStartNotification" object:nil userInfo:nil]; } @end
NewScene.h
@interface NewScene : SKScene @end
NewScene.m
@implementation NewScene @end