【问题标题】:How to transition scenes in Swift如何在 Swift 中转换场景
【发布时间】:2014-07-29 12:01:16
【问题描述】:

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

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 时,到目前为止我已经完成了这项工作......

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")
        }

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

  1. 谁能提供一个在 Swift 中使用 SKTransition 的示例?

谢谢

【问题讨论】:

  • 2. Swift 实际上使使用多个文件变得更容易,因为它会自动导入模块中的所有文件。没有任何理由不遵循与 Objective-C 相同的模式

标签: sprite-kit swift transitions ios8 xcode6


【解决方案1】:

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

那么对于你的第一个问题......是的,你已经有很多了。基本上,从你起床的地方,你需要通过它的 size: initializer 创建 GameScene 的实例。从那里,您只需设置属性并调用 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)
}

【讨论】:

  • 那行得通。谢谢你。只是给几乎和我一样挑战的人的注释......“GameScene(size:self.scene.size)”中的“GameScene”只是指您创建的SpriteKit文件名(它不是特殊的保留短语) .
  • 您要过渡的场景会发生什么?它会继续运行还是您需要故意暂停它?如果您正在从该场景过渡的场景中发生了某些动作,这似乎会影响您的游戏。
  • @user1639164 默认情况下,传出和传入场景都在过渡期间暂停。如果您希望更改此行为,可以通过更改 SKTransition 的 pausesIncomingScenepausesOutgoingScene 属性的值来完成。
  • 关于你的“过渡”:'Down' has been renamed to 'down' & 'revealWithDirection(_:duration:)' has been renamed to 'reveal(with:duration:)'。在 nextScene.scaleMode 上,'AspectFill' has been renamed to 'aspectFill'
【解决方案2】:

如果你必须在 touch-begain 或 node action 上工作,那么使用它:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        let touch = touches.anyObject() as UITouch
        if CGRectContainsPoint(btncloseJump.frame, touch.locationInNode(self)) {
            self.scene.removeFromParent()
            btncloseJump.removeFromParent()
            let skView = self.view as SKView
            skView.ignoresSiblingOrder = true
            var scene: HomeScene!
            scene = HomeScene(size: skView.bounds.size)
            scene.scaleMode = .AspectFill
            skView.presentScene(scene, transition: SKTransition.fadeWithColor(SKColor(red: 25.0/255.0, green: 55.0/255.0, blue: 12.0/255.0, alpha: 1), duration: 1.0))
        }
    }

【讨论】:

  • 这个答案对我也有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
相关资源
最近更新 更多