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

精灵套件 – 如何在Swift中过渡场景,第1张

概述在Objective-C中,使用Sprite-Kit,我会在Objective-C中成功地使用如下代码来创建一个新的场景 if ([touchedNode.name isEqual: @"Game Button"]) { SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDo 在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)        }    }}
总结

以上是内存溢出为你收集整理的精灵套件 – 如何在Swift中过渡场景全部内容,希望文章能够帮你解决精灵套件 – 如何在Swift中过渡场景所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1038872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存