cuốn sách gpt4 ai đã làm

swift - SpriteKit "START"在场景中调用 start() 的按钮

In lại Tác giả: Hồ Xil 更新时间:2023-11-01 02:10:30 26 4
mua khóa gpt4 Nike

我如何在 StartButton 类中编写一个 touchesBegun 命令,它在场景中调用 start() 任何实例本身?

我知道......可能是 OOP 101。但今天我远远超出了我的范围。

gia hạn:

这是我目前(部分)解决问题的方法。感觉不对,但有点用。没有我想要的那么多:

override func touchesBegan(_ touches: Set, with event: UIEvent?) {
SoundManager.playSceneStartSound()
run(ready)
if CEO.startButtonIsActive{
print("We're STARTING NOW...")
if let menuScene = self.scene as? Menu_Memory {
menuScene.startTheGame()
}
khác
if let menuScene = self.scene as? Menu_Chaser {
menuScene.startTheGame()
}
khác
if let menuScene = self.scene as? Menu_Picker {
menuScene.startTheGame()
}
khác
if let menuScene = self.scene as? Menu_Q_AndA {
menuScene.startTheGame()
}
khác
{print("Houston, we have a problem... no gameScene")}
}
khác
{
print("You still have some preparation to do, amigo!")
}
}

或者,传递类类型的能力也会有所帮助。这几乎是一个单独的问题,但事实上我认为它们可能非常密切相关。

无论如何:在按钮中,尝试创建对类类型的引用,我尝试了很多变体,但没有成功。

我的一些可笑的尝试,都失败了:

Menu_Memory 是一个 SKScene

    var menuClass01: Menu_Memory.self
var menuClass02: AnyClass = Menu_Memory.self
var menuClass03: AnyClass = Menu_Memory(self)
var menuClass04: AnyClass as! Menu_Memory.self
var menuClass05: AnyClass as! Menu_Memory(self)

câu trả lời hay nhất

gia hạn现在我知道你在做什么了。应该是不言自明的。如果不是,请对我大喊大叫:

public extension SKScene {
func startGame() { // Because startTheGame is a terriblename..
// Override in your subclasses of SKScene if desired.
}
}

enum Menu { // Used as namespace for chaser and menu classes

final class Chaser: SKScene {
override func startGame() {
print("chaser started")
}
}

final class Memory: SKScene {
override func startGame() {
print("memory started")
}
}
}

.

final class Button: SKNode {
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
scene!.startGame() // No need for guard / if-let,
// because how can you click the node if its scene is nil :)?
}
}

关于swift - SpriteKit "START"在场景中调用 start() 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41092440/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress