Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在场景中隐藏和取消隐藏按钮_Swift_Xcode_Sprite Kit_Skspritenode_Skscene - Fatal编程技术网

Swift 在场景中隐藏和取消隐藏按钮

Swift 在场景中隐藏和取消隐藏按钮,swift,xcode,sprite-kit,skspritenode,skscene,Swift,Xcode,Sprite Kit,Skspritenode,Skscene,在我和Swift一起开发的一个游戏中,我有一个场景,玩家可以查看不同的背景进行选择。背景填充在后面,有一些按钮可以让你看到下一个或上一个背景。我已经测试了一个“选择”按钮,它可以保存当前背景并转换到游戏场景。现在我想根据背景显示不同的“选择”按钮,每个按钮将显示不同的价格,并将从玩家的硬币中减去不同的金额 当玩家点击“下一步”和“上一步”按钮时,我的代码当前可以更改背面。但我在显示每个背面的“选择”按钮时遇到问题。以下是我的代码的相关部分: import SpriteKit class Sho

在我和Swift一起开发的一个游戏中,我有一个场景,玩家可以查看不同的背景进行选择。背景填充在后面,有一些按钮可以让你看到下一个或上一个背景。我已经测试了一个“选择”按钮,它可以保存当前背景并转换到游戏场景。现在我想根据背景显示不同的“选择”按钮,每个按钮将显示不同的价格,并将从玩家的硬币中减去不同的金额

当玩家点击“下一步”和“上一步”按钮时,我的代码当前可以更改背面。但我在显示每个背面的“选择”按钮时遇到问题。以下是我的代码的相关部分:

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中隐藏和取消隐藏按钮?

错误是因为您在
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)
    }
}

错误是因为您在
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)
    }
}