gpt4 book ai didi

swift - 在 SKScene 中隐藏和取消隐藏按钮

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

在我使用 Swift 开发的游戏中,我有一个 SKScene,玩家可以在其中查看不同的背景以供选择,然后选择一个。背景填满了背面,还有一些按钮可以让人们看到下一个或上一个背景。我已经测试了一个“选择”按钮,它可以保存当前背景并过渡到游戏场景。现在我想根据背景显示不同的“选择”按钮,每个按钮将显示不同的价格,并从玩家的硬币中减去不同的金额。

我的代码目前可以在玩家点击“下一个”和“上一个”按钮时更改后退。但是我无法显示每个后背的“选择”按钮。这是我的代码的相关部分:

import SpriteKit

class ShopScene: SKScene {

var backNumber = 100
var backRemainder = 0
var background = SKSpriteNode()

var coinNumber = UserDefaults.standard.integer(forKey: "coinSaved")
var backName:String? = UserDefaults.standard.string(forKey: "backSaved")

override func didMove(to view: SKView) {

if backName != nil {
backName = UserDefaults.standard.string(forKey: "backSaved")
} else {
backName = "back1"
}

background.texture = SKTexture(imageNamed: "\(backName!)")
self.addChild(background)

let nextButton: NButton = NButton(defaultButtonImage: "next", activeButtonImage: "nextP", buttonAction: nextAction)
addChild(nextButton)

let previousButton: PButton = PButton(defaultButtonImage: "previous", activeButtonImage: "previousP", buttonAction: previousAction)
addChild(previousButton)

let selectButton: SButton = SButton(defaultButtonImage: "select", activeButtonImage: "selectP", buttonAction: selectAction)
addChild(selectButton)

func nextAction() {

backNumber += 1
backRemainder = backNumber % 2

switch backRemainder {
case 0:
backName = "back1"
case 1:
backName = "back2"
selectButton.isHidden = true
default:
backName = "back1"
}

UserDefaults.standard.set(backName, forKey: "backSaved")
background.texture = SKTexture(imageNamed: "\(backName!)")
}

func previousAction() {

backNumber -= 1
backRemainder = backNumber % 2

switch backRemainder {
case 0:
backName = "back1"
case 1:
backName = "back2"
selectButton.isHidden = true
default:
backName = "back1"
}

UserDefaults.standard.set(backName, forKey: "backSaved")
background.texture = SKTexture(imageNamed: "\(backName!)")
}

如您所见,我正在尝试使用 isHidden 属性,但出现错误:“使用未解析的标识符‘selectButton’”。我试过在 didMove(toView) 之前初始化按钮,但它只是把事情搞砸了,因为 selectAction() 必须在 didMove(toView) block 之后。我希望我刚刚写的东西在某些方面不会太困惑或错误,我只是在学习使用 SpriteKit 编码。

如何在 SKScene 中隐藏和取消隐藏按钮?

最佳答案

错误是因为您在 didMove(to view: SKView) 函数中声明了按钮。 PreviousAction() 不会知道这些变量的存在。您需要将它们的声明移到类中而不是 func

class ShopScene: SKScene {

let nextButton: NButton!
let previousButton: PButton!
let selectButton: SButton!

override func didMove(to view: SKView) {

nextButton = NButton(defaultButtonImage: "next", activeButtonImage: "nextP", buttonAction: nextAction)
addChild(nextButton)

previousButton = PButton(defaultButtonImage: "previous", activeButtonImage: "previousP", buttonAction: previousAction)
addChild(previousButton)

selectButton = SButton(defaultButtonImage: "select", activeButtonImage: "selectP", buttonAction: selectAction)
addChild(selectButton)
}
}

关于swift - 在 SKScene 中隐藏和取消隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45068933/

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