SpriteKit如何创建和过渡到不同的场景

时间:2014-02-15 02:27:49

标签: sprite-kit

我创建了一个带有精灵套件的游戏但是在游戏启动时,它会直接进入游戏。我如何实现不同的场景,如主菜单和场景游戏,并通过在屏幕上按标签或在游戏过程中通过联系人在它们之间进行转换。

1 个答案:

答案 0 :(得分:14)

你可以尝试这样的事情。创建一个新类并随意调用它(我称之为GameStartMenu,并使其成为SKScene的子类)

在ViewController .m文件中,将MyScene替换为新的类名:

// Create and configure the scene.
SKScene * scene = [GameStartMenu sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;

然后在你的新课程.m中键入以下内容:

#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)。删除你需要的任何背景颜色等等。现在我也是编程的新手,所以这可能是完全错误的方法,但这对我来说是有用的。

相关问题