Reputation: 223
I have created a game with sprite kit but at the moment when the game is launched, it goes straight to the game. How can i implement different scenes like a main menu and a game over scene and transition between them either by pressing a label on the screen or by a contact during the game.
Upvotes: 3
Views: 5467
Reputation: 141
You could try something like this. Create a new class and call it whatever you want (I called mine GameStartMenu, and make it a subclass of SKScene)
In your ViewController .m file replace MyScene with the new class name:
// Create and configure the scene.
SKScene * scene = [GameStartMenu sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
Then in your new class .m type something like this:
#import "GameStartMenu.h"
#import "MyScene.h"
@implementation GameStartMenu
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:1.5 green:1.0 blue:0.5 alpha:0.0];
NSString *nextSceneButton;
nextSceneButton = @"Start";
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
myLabel.text = nextSceneButton;
myLabel.fontSize = 30;
myLabel.fontColor = [SKColor blackColor];
myLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
myLabel.name = @"scene button";
[self addChild:myLabel];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"scene button"]) {
SKTransition *reveal = [SKTransition fadeWithDuration:3];
MyScene *scene = [MyScene sceneWithSize:self.view.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:scene transition:reveal];
}
}
@end
This creates a label that when touched transitions to the new scene (MyScene). Delete whatever you need to like background color etc. Now I'm also new to programming so this could be completely the wrong way to do it but this works for me so far.
Upvotes: 14