如何在 Swift 中转换场景 [英] How to transition scenes in Swift

查看:16
本文介绍了如何在 Swift 中转换场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Objective-C 中,使用 Sprite-Kit,我会成功在 Objective-C 中使用类似以下代码的代码来调出一个新场景

In Objective-C, using Sprite-Kit, I would successfully use something like the following code in Objective-C to bring up a new scene

if ([touchedNode.name  isEqual: @"Game Button"]) {
        SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
        GameScene *newGameScene = [[GameScene alloc] initWithSize: self.size];
        //  Optionally, insert code to configure the new scene.
        newGameScene.scaleMode = SKSceneScaleModeAspectFill;
        [self.scene.view presentScene: newGameScene transition: reveal];
    }

在尝试将我的简单游戏移植到 Swift 时,到目前为止我已经完成了这项工作......

In trying to port my simple game to Swift, so far I have this working...

for touch: AnyObject in touches {
        let touchedNode = nodeAtPoint(touch.locationInNode(self))
        println("Node touched: " + touchedNode.name);
        let touchedNodeName:String = touchedNode.name

        switch touchedNodeName {
        case "Game Button":
            println("Put code here to launch the game scene")

        default:
            println("No routine established for this")
        }

但我不知道要编写什么代码才能真正过渡到另一个场景.问题:

But I do not know what code to write to actually transition to another scene. Question(s):

  1. 谁能提供一个在 Swift 中使用 SKTransition 的示例?
  2. 您通常会创建另一个文件"以将另一个场景代码放入另一个场景中,假设您会在 Objective-C 下使用,还是使用 Swift 有什么地方意味着我应该以不同的方式处理它?<​​/li>
  1. Can someone please provide an example of using SKTransition with Swift?
  2. Would you normally create another "file" to put the other scene code in for the other scene, assuming you would have under Objective-C, or is there something about using Swift that means I should approach it differently?

谢谢

推荐答案

从你的第二个问题开始,这取决于你.如果您愿意,您可以继续遵循每个文件拥有一个类的 Objective-C 约定,尽管这不是必需的,并且在 Objective-C 中也没有.话虽如此,如果您有几个密切相关的类,但不是由太多代码组成,那么将它们分组在一个文件中并不是不合理的.做正确的事,不要在单个文件中编写大量代码.

To start with your second question, it's kind of up to you. If you wish to, you can continue to follow the Objective-C convention of having one class per file, although this isn't a requirement, and wasn't in Objective-C either. That being said, if you have a couple of classes that are tightly related, but aren't made up of much code, it wouldn't be unreasonable to have them grouped in a single file. Just do what feels right, and don't make a huge blob of code in a single file.

那么对于你的第一个问题......是的,你已经有很多了.基本上,从你起身的地方,你需要通过它的 size: initializer 创建 GameScene 的实例.从那里,您只需设置属性并调用 present.

Then for your first questions... Yes, you had a good deal of it already. Basically, from where you got up to, you need to create the instance of GameScene through its size: initializer. From there, you just set the properties and call present.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    super.touchesBegan(touches, withEvent: event)

    guard let location = touches.first?.locationInNode(self),
        let scene = scene,
        nodeAtPoint(location) == "Game Button" else {
        return
    }

    let transition = SKTransition.reveal(
        with: .down, 
        duration: 1.0
    )

    let nextScene = GameScene(size: scene.size)
    nextScene.scaleMode = .aspectFill

    scene.view?.presentScene(nextScene, transition: transition)
}

这篇关于如何在 Swift 中转换场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆