gpt4 book ai didi

sprite-kit - SpriteKit : How to identify the previous scene the user is coming from?

转载 作者:行者123 更新时间:2023-12-01 23:12:41 25 4
gpt4 key购买 nike

我有一个 GameOver 场景,其中包含一个指向“设置”场景的按钮。 (下面显示的代码)我想在 MainMenu 场景上添加一个按钮来执行相同的操作,将用户发送到 Settings 场景,但我只想在 Settings 场景上添加一个“后退”按钮。我假设这可以通过 IF 语句来完成。如果用户来自 A 场景则移动到 B 否则如果用户来自 X 场景则移动到 Y 场景。

我如何在 SpriteKit 中判断用户来自哪个场景?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch: AnyObject in touches {

let pointOfTouch = touch.location(in: self)

let nodeUserTapped = atPoint(pointOfTouch)

if nodeUserTapped.name == "BackButton" {

let sceneToMoveTo = SKScene(fileNamed: "GameOverScene")
let gameTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo!, transition: gameTransition)

}

}

}

最佳答案

在这种情况下,您可以采用各种解决方案来实现您的愿望。我只想向您展示 2 个示例,一种是快速简单的方法,另一种是更自定义的方法。


1: - 您可以使用 userData下一个场景的实例属性来存储当前场景,例如:

sceneToMoveTo.userData = NSMutableDictionary()
sceneToMoveTo.userData?.setObject("MenuScene", forKey: "previousScene" as NSCopying)

当你在下一个场景时,你可以问这个 NSMutableDictionary 谁是前一个场景:

guard let previousValue = self.userData?.value(forKey: "previousScene") else { return }
switch previousValue as! String {
case "MenuScene":
//do whatever you want if the previous scene was MenuScene
default:
break
}

2: - 您可以构建一个枚举,其中包含您要移动到的场景列表。

之后,每个场景都包含一个简单的 var 来存储前一个场景:默认为 .None 但您应该在调用新场景时填充它。当您到达 SettingsScene 并按下后退按钮时,您可以询问这个 previousScene var 谁是前一个场景,并通过 switch 语句决定去哪里。

// GAMEOVER SCENE
import SpriteKit
enum Scenes : Int {
case MainMenu = 0
case Settings = 1
case GameOver = 2
case None = 3
}
class GameOver: SKScene {
var previousScene:Scenes = .None
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeUserTapped = atPoint(pointOfTouch)
if nodeUserTapped.name == "settingsBtn" {
if let sceneToMoveTo = SKScene(fileNamed: "SettingsScene") {
let gameTransition = SKTransition.fade(withDuration: 0.5)
(sceneToMoveTo as! SettingsScene).previousScene = .GameOver
self.view?.presentScene(sceneToMoveTo, transition: gameTransition)
}
}
}
}
}
// SETTINGS SCENE
import SpriteKit
class SettingsScene: SKScene {
var previousScene:Scenes = .None
override func didMove(to view: SKView) {
print("Current scene loaded: \(type(of:self))")
print("Previous scene was: \(previousScene)")
let label = SKLabelNode.init(text: "BackButton")
addChild(label)
label.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
label.name = "backBtn"
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeUserTapped = atPoint(pointOfTouch)
if nodeUserTapped.name == "backBtn" {
switch previousScene {
case .MainMenu: // go back to menu
if let sceneToMoveTo = SKScene(fileNamed: "MenuScene") {
let gameTransition = SKTransition.fade(withDuration: 0.5)
(sceneToMoveTo as! MenuScene).previousScene = .Settings
self.view?.presentScene(sceneToMoveTo, transition: gameTransition)
}
break
case .GameOver: // re-launch a new game
print("The game was ended")
if let sceneToMoveTo = SKScene(fileNamed: "GameScene") {
let gameTransition = SKTransition.fade(withDuration: 0.5)
(sceneToMoveTo as! GameScene).previousScene = .Settings
self.view?.presentScene(sceneToMoveTo, transition: gameTransition)
}
default:
break
}
}

}
}
}
//MENU SCENE
import SpriteKit
class MenuScene: SKScene {
var previousScene:Scenes = .None
override func didMove(to view: SKView) {
print("Current scene loaded: \(type(of:self))")
print("Previous scene was: \(previousScene)")
}
}

关于sprite-kit - SpriteKit : How to identify the previous scene the user is coming from?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41497466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com