SpriteKit how to create and move to different scenes

I created a game with a set of sprites, but the moment the game is running, it goes straight into the game. How can I implement various scenes, such as the main menu and the game above the scene, and the transition between them either by tapping a label on the screen or by contact during the game.

+3
source share
1 answer

You can try something like this. Create a new class and name it whatever you want (I named my GameStartMenu and will 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 .m class, enter 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

, (MyScene). , , , .. , , .

+14

All Articles