gpt4 book ai didi

ios - 将当前 GameScene 实例传递到在 GameScene 中调用的类中

转载 作者:行者123 更新时间:2023-11-28 12:43:02 25 4
gpt4 key购买 nike

我正在我的 SpriteKit 游戏中构建一个 PauseButton 类,我想在 GameScene 激活时绘制一个标签。但是,我需要在 didMoveToView 之外实例化按钮(就在它之前 - 在 GameScene 中),以便我可以从 更“全局”地访问它touchBegan 方法。

let pauseButton = PauseButton(theTexture: pauseButtonTexture,gameScene:GameScene)
override func didMoveToView(view: SKView) {

理想情况下,我可以通过在 didMoveToView 中实例化它来传递 View ,但截至目前,我不知道如何执行此操作以及如何从场景的其余部分访问它。也就是说,有没有办法传入我正在使用的当前 GameScene 实例,以便我可以从方法内部执行类似 gameScene.addchild(label) 的操作我的按钮类?我找不到任何有用的东西,所以任何帮助都会很棒!

最佳答案

你基本上可以通过两种方式做到这一点。

1) 像这样创建属性

var pauseButton: PauseButton!

然后像这样在 didMoveToView 中实例化它。

override func didMoveToView(view: SKView) {
pauseButton = PauseButton(theTexture: pauseButtonTexture,gameScene: self)
addChild(pauseButton)
}

2) 使用惰性实例化,这样您就可以做您最初尝试做的事情。

最简单形式的惰性变量允许您在不执行第 1 步的情况下使用 self。

lazy var pauseButton: PauseButton = PauseButton(theTexture: pauseButtonTexture,gameScene: self)

override func didMoveToView(view: SKView) {

您甚至可以像这样在同一位置添加此按钮的更多属性。

lazy var pauseButton: PauseButton = {
let button = PauseButton(theTexture: pauseButtonTexture,gameScene: self)
button.zPosition = 200
button.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
button.alpha = 0.5

return button
}()

override func didMoveToView(view: SKView) {
addChild(pauseButton)
}

记住每个 SKNode/SKSpriteNode 等都有一个场景属性。如果您的按钮类是 SKNode/SKSpriteNode 的子类,那么您可以获取其场景属性并使用“as”运算符获取对 GameScene 的引用,而无需在初始化方法中传入场景。

例如

class GameScene: SKScene {

lazy var pauseButton: PauseButton = PauseButton(theTexture: pauseButtonTexture,gameScene: self)

var someBool = false

override func didMoveToView(view: SKView) {
addChild(pauseButton)
pauseButton.loadPausedLabel()
}

func someMethod() {

}
}

class PauseButton: SKSpriteNode {

func loadPauseLabel() {

// Way 1, reference to current scene but not to specific class

guard let scene = scene else { return }
// Scene property is optional and is nil until button is added to a Scene so add the guard check.


scene.someBool = true // NOT WORKING
scene.someMethod() // NOT WORKING

let label = SKLabelNode(...
scene.addChild(label) // WORKING

// Way 2, use "as" to cast to specific scene (e.g GameScene) so you can call properties/methods on it. This is the same as passing GameScene in the init method.

guard let gameScene = scene as? GameScene else { return }
// Scene property is optional and is nil until button is added to a Scene so add the guard check.
// The button must be added to the scene you are trying the as? cast with, in this example GameScene.


gameScene.someBool = true // WORKING
gameScene.someMethod() // WORKING

let label = SKLabelNode(...
gameScene.addChild(label) // WORKING
}
}

作为提示,我不会在 pauseButton 子类中添加暂停标签,我会从场景本身或暂停菜单子(monad)类中添加它。此外,我不会对每个按钮进行子类化,我会创建 1 个可用于游戏中所有按钮的类。您可以创建一个带有按钮名称的枚举,并将其传递到 init 方法中,以在按下按钮时区分按钮。

您的按钮子类应该只处理动画、纹理等。您可以查看 apples game DemoBots 作为示例。

希望对你有帮助

关于ios - 将当前 GameScene 实例传递到在 GameScene 中调用的类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38959540/

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