SpriteKit如何创建和过渡到不同的场景(Swift语言)

时间:2014-06-15 13:49:24

标签: swift sprite-kit transition

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

4 个答案:

答案 0 :(得分:3)

您似乎没有查看过Apple的文档。他们有一个很好的SpriteKit指南。此外,spriteKit类别中RayWenderlich.com的教程非常出色。我还没有读过很多关于Swift的内容,但我的理解是Swift可以与Objective-C一起编写。话虽如此,我会给你一个起点(在Objective-C中)。

1)调用初始场景:

在主视图控制器中,您需要将VC的view属性转换为SKView内的viewDidLoad,如下所示:

SKView *spriteKitView = (SKView *)self.view;

SKView上的一些有用的调试属性:

spriteKitView.showsFPS = YES;
spriteKitView.showsNodeCount = YES;
spriteKitView.showsPhysics = YES;

您必须单独配置每个场景(子类SKScene)。完成此操作后,将主场景导入主VC的类实现。要呈现主场景(仍在viewDidLoad中):

MyScene *mainScene = [MyScene new];
[spriteKitView presentScene: mainScene];

2)通过标签(或任何其他SKNode)在场景之间转换: 将相关节点设置为属性,并通过以下方式显示新场景:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];

    if ([self.playAgainButton containsPoint:touchLocation]) {
        [self presentGamePlayScene];
    } else if([self.mainMenuButton containsPoint:touchLocation]) {
        [self presentMainMenu];
    }
}


- (void)presentGamePlayScene {
    gamePlayScene *newGamePlay = [[gamePlayScene alloc]init];
    newGamePlay.scaleMode = SKSceneScaleModeAspectFill;
    SKTransition *arbitraryTransition = [SKTransition crossFadeWithDuration:2.0]; //look into SKTransition for a plethora of different options.
    [self.view presentScene:newGamePlay transition:arbitraryTransition];
}

3)要通过联系人更改场景,请遵守任何给定SKPhysicsContactDelegate内的协议SKScene。在该场景的实施中:

- (void)didBeginContact:(SKPhysicsContact *)contact {
    if(contact.bodyA.categoryBitMask == enemyCategory && contact.bodyB.categoryBitMask == arbitraryNodeCategory) {
        [self presentGameOverScene];
    }
}

SKPhysicsContact通过属性附加了2个碰撞SKPhysicsBody。这些碰撞节点必须初始化SKPhysicsBody。此外,他们必须在contactBitMask上设置SKPhysicsBody属性,如此:

@implementation SomeNode 

- (instanceType)initCollidingNode {
     if(self = [super init]) {
         self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.someSize];
         self.physicsBody.contactBitMask = enemyCategory | powerUpCategory;
         self.physicsBody.categoryBitMask = arbitraryNodeCategory;
         self.physicsBody.dynamic = YES 
     }
     return self;
}
  • SKSpriteNode和SKPhysicsBody具有您可能需要的其他相关属性

答案 1 :(得分:1)

我在swift中解决了这个问题。

func goToGameScene(){
    let gameScene:GameScene = GameScene(size: self.size) // create your new scene
    let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 0) // create type of transition (you can check in documentation for more transtions)
    gameScene.scaleMode = SKSceneScaleMode.AspectFill
    self.view!.presentScene(gameScene, transition: transition)
}

答案 2 :(得分:0)

创建一个名为:TestScene.Swift的文件

在GameScene.Swift文件中声明它并实现touches函数,如下所示:

var sceneTest: TestScene!

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    // Configure the view.
    let gameView = self.view as SKView!

    /* Sprite Kit applies additional optimizations to improve rendering performance */
    gameView.ignoresSiblingOrder = true

    sceneTest = TestScene(size: gameView.bounds.size)
    gameView.presentScene(sceneTest)
    sceneTest.scaleMode = .AspectFill
}

这将显示一个名为TestScene的新场景

答案 3 :(得分:0)

创建一个名为MainMenu.swift的新swift文件

以下代码放置在当前场景中,允许转换到MainMenu.swift中描述的场景:

var scene =  MainMenu(size: self.size)
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)

在新场景中,您可以使用以下代码返回GameScene.swift中描述的场景:

var scene =  GameScene(size: self.size)
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)