隐藏和取消隐藏SKScene中的按钮

时间:2017-07-12 23:11:53

标签: swift xcode sprite-kit skspritenode skscene

在我使用Swift开发的游戏中,我有一个SKScene,玩家可以在其中查看不同的背景供您选择并选择一个。背景填充背面,有一些按钮,可以让人看到下一个或以前的背景。我已经测试了一个' select'保存当前背景并转换到游戏场景的按钮。现在我想展示不同的选择'按钮取决于背景,每个按钮将显示不同的价格,并将减少不同的金额给玩家的硬币。

当玩家点击“下一个”时,我的代码目前可以改变回来状态。和之前的'纽扣。但是我在显示“选择”时遇到了问题。每个背面的按钮。这是我的代码的相关部分:

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)块之后才搞砸了。我希望我刚写的内容在某些方面不会太混乱或错误,我只是学习使用SpriteKit进行编码。

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

1 个答案:

答案 0 :(得分:2)

错误是因为您在didMove(to view: SKView) func中声明了按钮。 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)
    }
}