In a game I'm developing with Swift, I have a SKScene in which the player can view different backgrounds to choose from and select one. The backgrounds fill the back and there are buttons that allow one to see the next or previous backgrounds. I've already tested a 'select' button that saves the current background and makes a transition to the Game Scene. Now I want to show different 'select' buttons depending on the background, each button will show a different price and will subtract a different amount to the player's coins.
My code can currently change the back when the player taps the 'next' and 'previous' buttons. But I'm having trouble showing the 'select' buttons for each back. Here's the relevant parts of my code:
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!)")
}
As you can see, I'm trying to use the isHidden property but I'm getting an error: "use of unresolved identifier 'selectButton'". I've tried initializing the button before didMove(toView) but it just messes things up as the selectAction() must be after the didMove(toView) block. I hope what I just wrote is not too confusing or wrong in some ways, I'm just learning to code with SpriteKit.
How can I hide and unhide buttons in a SKScene?
The error is because you are declaring your buttons in the didMove(to view: SKView)
func. PreviousAction()
won't know that those variables exist. You need to move their declarations up inside of the class not the 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)
}
}