在 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")
}
但我不知道要写什么代码才能真正过渡到另一个场景。 问题:
- 有人可以提供一个在 Swift 中使用 SKTransition 的例子吗?
- 假设您使用的是 Objective-C,您通常会创建另一个"file"来将其他场景代码放入另一个场景吗?或者是否有关于使用 Swift 的一些东西意味着我应该以不同的方式处理它?</li >
谢谢
最佳答案
从您的第二个问题开始,这完全取决于您。如果您愿意,您可以继续遵循每个文件一个类的 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)
}
关于sprite-kit - 如何在 Swift 中转换场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24111935/