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

标签 swift xcode sprite-kit skspritenode skscene

在我使用 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/

相关文章:

ios - 直接使用 Realm 对象还是映射到对象类?

xcode - 在 Swift 中将 Parse Core 链接到 Apple Watch

ios - 将 Codable 结构编码为 JSON 后如何存储数据

ios - 在自动布局中,如何将 "hide"设置为 UIView?

swift - 未从 firebase 数据库 swift 4 中获取数据

ios - 如何在 Xcode 4 的 Zombies 工具中找出僵尸 CALayer 属于哪个 UIView?

swift - iMac 在 while 循环后卡住

swift - SKAction 遵循 UIBezierPath 函数不起作用 SpriteKit - Swift

swift - 在正确的时间停止纹理动画 SKAction 并在 Spritekit Swift 中启动另一个纹理动画

arrays - Swift 使用多个排序条件对数组进行排序