微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

精灵套件 – 如何在Swift中过渡场景

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

但是我不知道要写什么代码来实际转换到另一个场景.
问题(S):

有人可以提供一个使用SKTransition与Swift的例子吗?
>通常会创建另一个文件”,将另一个场景代码放在另一个场景中,假设您将在Objective-C下使用,或者有什么关于使用Swift,这意味着我应该以不同的方式处理它?

谢谢

从你的第二个问题开始,这取决于你.如果你愿意,你可以继续遵循Objective-C惯例,每个文件一个类,尽管这不是一个要求,也不在Objective-C中.话虽如此,如果你有几个紧密相关的类,但不是由很多代码组成,将它们分组在一个文件中是不合理的.只要做什么感觉就是正确的,不要在一个文件中制作一大堆代码.

那么对于你的第一个问题…是的,你已经有了很多的事情.基本上,从哪里起,您需要通过其大小:initializer创建GameScene的实例.从那里,您只需设置属性调用存在.

override func touchesBegan(touches: NSSet!,withEvent event: UIEvent!) {
    super.touchesBegan(touches,withEvent: event)

    let location = touches.anyObject().locationInNode(self)
    let touchednode = self.nodeAtPoint(location)

    if touchednode.name == "Game Button" {
        let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down,duration: 1.0)

        let nextScene = GameScene(size: self.scene.size)
        nextScene.scaleMode = SKScenescaleMode.AspectFill

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

或在现代Swift:

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

    if let location = touches.first?.locationInNode(self) {
        let touchednode = nodeAtPoint(location)

        if touchednode.name == "Game Button" {
            let transition = SKTransition.revealWithDirection(.Down,duration: 1.0)

            let nextScene = GameScene(size: scene!.size)
            nextScene.scaleMode = .AspectFill

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

原文地址:https://www.jb51.cc/swift/319682.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐