How do I get this if statement to work?

I have a SKSpritnode table for a background in a game and I want the background to change as the players points reach a certain level eg every 20 points. I use a range operator as shown below. The code below builds and runs but background doesn't change.

Code Block
func setupTable(){
if(0...19).contains(playerScore) {
let table = SKSpriteNode(imageNamed: "ovaloffice")
addChild(table)
table.position = CGPoint(x: size.width/2, y: size.height/2)
table.zPosition = -1}
else if(20...39).contains(playerScore) {
let table = SKSpriteNode(imageNamed: "austin")
addChild(table)
table.position = CGPoint(x: size.width/2, y: size.height/2)
table.zPosition = -1}
else {
let table = SKSpriteNode(imageNamed: "bond")
addChild(table)
table.position = CGPoint(x: size.width/2, y: size.height/2)
table.zPosition = -1}
func addPlayerScore() {
playerScore += 1
playerLabel.text = "Arn: \(playerScore)"
}


Answered by OOPer in 639082022

I had to put the Extension out of the scope

Sorry, not being clear enough. Can you see the indentation of my code? I make my code properly indented and copied the code into post keeping the indentation.

You should better care about indentation of your code.
Use Cmd-A (Select All) and Ctrl-I (Re-Indent)

Starting from column #1 in my code means the definition is not in any other types.
Put the definition of enum TableType and the extension into some Swift file of your project, but not inside GameScene or any other types.
Move them out of GameScene.
First of all, in your shown code, braces are not balancing.
I guess you need to put a closing brace at line 17.

Second, your setupTable adds a new SKSpriteNode depending on the value of playerScore at the time of call.

If you want your background to be changed according to playerScore, you need to run a code to change the background at each time playerScore changes.
As OOPer said, you miss closing bracket on line 17 to end the else statement. In fact you miss a second one line 18 to end the setUpTable func. Because I doubt that func addPlayerScore() must be defined inside setUpTable as it it never called there.
Surprised the code compile like this.
Thanks for the input. The brackets that are missing I have just missed when I pasted the code onto the forum. Apolagies.
I want the background image to change when players points reach 20 and then again at 40 points and so on.
You are correct the addPlayerScore function is defined elsewhere I just wanted to include the function in case the best way to get the results I am looking for is to put the code inside the addPlayerScore function.
I am very new to coding so its a lot of trial and error at the moment.

I want the background image to change when players points reach 20 and then again at 40 points and so on.

Have you achieved what you want? If not yet, please show your latest code (without missing something) with other relevant parts.
Hi OOper. Thanks for taking the time to look at it. I haven't managed to get it working yet. Its driving me crazy. Its a simple blackjack game with audio samples that play as game scenarios come up in the game.
The whole code for setUpTable function is below along with the game scene. The project builds and runs but the Spritenode stays on "ovaloffice". I tried switching the Spritenodes around but whatever image is placed in line 72 is what appears and doesn't change at the 20 point mark. Apolagies if there is more than you need regarding code.

Code Block
class GameScene: SKScene {
    let moneyContainer = SKSpriteNode(color: .clear, size: CGSize(width:250, height: 150))
    let dealBtn = SKSpriteNode(imageNamed: "deal_btn")
    let hitBtn = SKSpriteNode(imageNamed: "hit_btn")
    let standBtn = SKSpriteNode(imageNamed: "stand_btn")
    let instructionText = SKLabelNode(text: "Place your bet")
    let player1 = Player(hand: Hand(),bank: Bank())
    let dealer = Dealer(hand: Hand())
    var allCards = [Card]()
    let dealerCardsY = 830
    let playerCardsY = 200
    var currentPlayerType:GenericPlayer = Player(hand: Hand(),bank: Bank())
    var playerYields = false
    let deck = Deck()
    let musicPlayer = MusicPlayer()
    var playerScore = 0
    let playerLabel = SKLabelNode(fontNamed: "AvenirNext-Bold")
    var dealSscore = 0
    let dealerLabel = SKLabelNode(fontNamed: "AvenirNext-Bold")
    let playerBlackjackSounds = ["looks", "my seat2", "knicks", "daddy money", "cheeto", "whipped"]
    let playerBustSounds = ["mail order", "winning not", "crayon", "on the chin", "move lips ah", "stupid", "food chain", "gooder", "teeth"]
    let dealerBlackjackSounds = ["35 bj", "ping pong", "whitejack", "piss off", "chicken", "can i say", "not whitejack", "blow mouth"]
    let arrayTieSounds = ["knicks", "rig it", "kings", "ping pong", "on the chin", "gooder"]
    let playerLoseSounds = ["stump up pb", "lose to donald", "rig it", "sarcasm", "hooker", "hilarys hand", "kings", "beat ass", "happy meal"]
    let dealerBustSounds = ["be back da", "won again", "leather", "thick b", "my seat", "d duck", "lost conc", "russian"]
    let dealerLoseSounds = ["lose better", "hilary desk", "not ripped", "chunky ass", "easy come", "you win im rich", "loose change", "whipped"]
    
override func didMove(to view: SKView) {
        setupTable()
        setupButtons()
        currentPlayerType = player1
        didTapButton()
        }
func setupTable(){   
       if(0...19).contains(playerScore) {
            let table = SKSpriteNode(imageNamed: "ovaloffice")
            addChild(table)
            table.position = CGPoint(x: size.width/2, y: size.height/2)
            table.zPosition = -1}
        else if(20...39).contains(playerScore)  {
            let table = SKSpriteNode(imageNamed: "austin")
            addChild(table)
            table.position = CGPoint(x: size.width/2, y: size.height/2)
            table.zPosition = -1}
        else {
            let table = SKSpriteNode(imageNamed: "bond")
            addChild(table)
            table.position = CGPoint(x: size.width/2, y: size.height/2)
            table.zPosition = -1}
        addChild(moneyContainer)
        moneyContainer.anchorPoint = CGPoint(x:0, y:0)
        moneyContainer.position = CGPoint(x:size.width/2 - 125, y:size.height/2)
        instructionText.fontColor = UIColor.red      
        instructionText.fontName = "AvenirNext-Bold"                    
        addChild(instructionText)
        instructionText.position = CGPoint(x: size.width/2, y: 400)  //(x: size.width/2, y: 400)
        deck.new()
       
       
        playerLabel.text = "Arnie: 0"
        playerLabel.fontSize = 30
        playerLabel.fontColor = SKColor.red
        playerLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left
        playerLabel.position = CGPoint(x: 350, y: 300)  // 350   360
        playerLabel.zPosition = 100
        addChild(playerLabel)
        dealerLabel.text = "Trump: 0"
        dealerLabel.fontSize = 30
        dealerLabel.fontColor = SKColor.red
        dealerLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left
        dealerLabel.position = CGPoint(x: 340, y: 660)  // was 300   660
        dealerLabel.zPosition = 100
        addChild(dealerLabel)
    }
func addPlayerScore() {
      playerScore += 1
      playerLabel.text = "Arnie: \(playerScore)"
  }
}





The whole code for setUpTable function is below along with the game scene.

Thanks for showing your code. That will help understanding what is your current problem and thinking how to fix it.

As I wrote before, you need to run a code to change the background at each time playerScore changes.
  • There's no code to change the background in your GameScene.

  • You are not doing anything to change the background on playerScore changes.

(Your setupTable() is called only once when didMove(to:) is executed, not on playerScore changes.)


For adding a code to change the background, you should better define a type representing the type of the background.
Code Block
enum TableType: String {
case ovaloffice
case austin
case bond
}
extension TableType {
static func `for`(score: Int) -> TableType {
switch score {
case 0...19:
return .ovaloffice
case 20...29:
return .austin
default:
return .bond
}
}
var imageName: String {rawValue}
}


And to change the background a little bit efficiently at each time playerScore changes, you need some properties to add and modify one property.
Code Block
var tableType: TableType = .ovaloffice
var playerScore = 0 {
didSet(newScore) {
let newTableType = TableType.for(score: newScore)
//Call `replaceTable()` only on `tableType` did change
if tableType != newTableType {
tableType = newTableType
replaceTable()
}
}
}
var currentTable: SKSpriteNode?


The method replaceTable() would become something like this:
Code Block
func replaceTable() {
//You need to remove existing `table` before adding new one
if let table = currentTable {
table.removeFromParent()
currentTable = nil
}
let table = SKSpriteNode(imageNamed: tableType.imageName)
addChild(table)
table.position = CGPoint(x: size.width/2, y: size.height/2)
table.zPosition = -1
//If you need to modify some properties based on the `tableType`, uncomment the following switch statement
/*
switch tableType {
case .ovaloffice:
//...
break
case .austin:
//...
break
case .bond:
//...
break
}
*/
}

You can modify your setupTable() using it:
Code Block
func setupTable() {
replaceTable()
addChild(moneyContainer)
moneyContainer.anchorPoint = CGPoint(x:0, y:0)
//...
}


The code above is not tested and I may be missing something important to show, but please try.
If you find something wrong with my code, please tell me what's going wrong in detail.
thanks OOPer , I will give the code a bash over the weekend and try to get it working. looking at the parts I understand I think I will be able to use this also for the final part off the APP which is changing the audio arrays as points change also.
Really appreciate the help.
Hi OOPer, I hope all is well.
I have tried putting the enum and functions into the project but I could not get it to work. I had to put the Extension out of the scope to stop an error flagging up so I might have put it in the wrong place.

If you can still help I have put a link below to the gamescene in GitHub which might give a better over sight on the project.

https://github.com/badgerstreak/Blackjack-in-the-House-copy/blob/main/Blackjackk/GameScene.swift

Sorry for the delay but I had a cycling mishap and broke my ankle so I have been hobbling about for a few weeks. The code you suggested is at the bottom of the game scene .

Thanks Joe
Accepted Answer

I had to put the Extension out of the scope

Sorry, not being clear enough. Can you see the indentation of my code? I make my code properly indented and copied the code into post keeping the indentation.

You should better care about indentation of your code.
Use Cmd-A (Select All) and Ctrl-I (Re-Indent)

Starting from column #1 in my code means the definition is not in any other types.
Put the definition of enum TableType and the extension into some Swift file of your project, but not inside GameScene or any other types.
Move them out of GameScene.
How do I get this if statement to work?
 
 
Q