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