In Objective-C, using Sprite-Kit, I would successfully use something like the following code in Objective-C to bring up a new scene
在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];
}
In trying to port my simple game to Swift, so far I have this working...
在尝试将我的简单游戏移植到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")
}
But I do not know what code to write to actually transition to another scene. Question(s):
但我不知道要写什么代码来实际转换到另一个场景。问题(S):
- Can someone please provide an example of using SKTransition with Swift?
- 有人可以提供一个使用Swift的SKTransition的例子吗?
- Would you normally create another "file" to put the other scene code in for the other scene, assuming you would have under Objective-C, or is there something about using Swift that means I should approach it differently?
- 您是否通常会创建另一个“文件”以将其他场景代码放入另一个场景中,假设您将使用Objective-C,或者是否有关于使用Swift的内容,这意味着我应该以不同的方式处理它?
Thank you
谢谢
2 个解决方案
#1
39
To start with your second question, it's kind of up to you. If you wish to, you can continue to follow the Objective-C convention of having one class per file, although this isn't a requirement, and wasn't in Objective-C either. That being said, if you have a couple of classes that are tightly related, but aren't made up of much code, it wouldn't be unreasonable to have them grouped in a single file. Just do what feels right, and don't make a huge blob of code in a single file.
从你的第二个问题开始,这取决于你。如果您愿意,您可以继续遵循Objective-C约定,即每个文件只有一个类,尽管这不是必需的,也不在Objective-C中。话虽这么说,如果你有几个紧密相关的类,但不是由很多代码组成,将它们分组在一个文件中是不合理的。只做正确的事情,不要在一个文件中制作大量的代码。
Then for your first questions... Yes, you had a good deal of it already. Basically, from where you got up to, you need to create the instance of GameScene through its size: initializer. From there, you just set the properties and call present.
那么对于你的第一个问题......是的,你已经有很多了。基本上,从你到达的地方,你需要通过它的大小创建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)
}
#2
3
if you have to work on touch-begain or node action , Then use it:
如果你必须处理touch-begain或节点动作,那么使用它:
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))
}
}
#1
39
To start with your second question, it's kind of up to you. If you wish to, you can continue to follow the Objective-C convention of having one class per file, although this isn't a requirement, and wasn't in Objective-C either. That being said, if you have a couple of classes that are tightly related, but aren't made up of much code, it wouldn't be unreasonable to have them grouped in a single file. Just do what feels right, and don't make a huge blob of code in a single file.
从你的第二个问题开始,这取决于你。如果您愿意,您可以继续遵循Objective-C约定,即每个文件只有一个类,尽管这不是必需的,也不在Objective-C中。话虽这么说,如果你有几个紧密相关的类,但不是由很多代码组成,将它们分组在一个文件中是不合理的。只做正确的事情,不要在一个文件中制作大量的代码。
Then for your first questions... Yes, you had a good deal of it already. Basically, from where you got up to, you need to create the instance of GameScene through its size: initializer. From there, you just set the properties and call present.
那么对于你的第一个问题......是的,你已经有很多了。基本上,从你到达的地方,你需要通过它的大小创建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)
}
#2
3
if you have to work on touch-begain or node action , Then use it:
如果你必须处理touch-begain或节点动作,那么使用它:
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))
}
}