-2

I'm trying to change the image of the SKSpriteNode in my shop scene when an upgrade is bought, I believe I'm on the right track with the variable, but I think I am doing it wrong. Thanks for the help in advanced, I've been trying for a while to figure it out with no luck.

//  ShopScene.swift
//  TheLastFlight
//
//  Created by -Zachary Walensa- on 8/3/16.
//  Copyright © 2016 -Zachary Walensa-. All rights reserved.
//

import Foundation
import SpriteKit

var coinUpgrade = defaults.integer(forKey: "coinUpgradeSaved")
var missileUpgrade = defaults.integer(forKey: "missileUpgradeSaved")

class ShopScene: SKScene {
var coinLabel = SKLabelNode(fontNamed: "The Bold Font")
var missileUpgrades = SKSpriteNode(imageNamed: "missile1")

override func didMove(to view: SKView) {

    coinLabel.text = "Coins: \(coinNumber)"
    coinLabel.fontSize = 100
    coinLabel.fontColor = SKColor.black
    coinLabel.zPosition = 1
    coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
    self.addChild(coinLabel)

    let background = SKSpriteNode(imageNamed: "background")
    background.size = self.size
    background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
    background.zPosition = 0
    self.addChild(background)

    let mainMenu = SKLabelNode(fontNamed: "The Bold Font")
    mainMenu.text = "Main Menu"
    mainMenu.fontSize = 100
    mainMenu.fontColor = SKColor.darkGray
    mainMenu.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
    mainMenu.zPosition = 1
    mainMenu.name = "Main Menu"
    self.addChild(mainMenu)

    let Life = SKSpriteNode(imageNamed: "lifeButton")
    Life.position = CGPoint(x: self.size.width*0.3, y: self.size.height*0.8)
    Life.zPosition = 1
    Life.name = "Life"
    self.addChild(Life)

    let coinUpgrades = SKSpriteNode(imageNamed: "coinUpgrade")
    coinUpgrades.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.8)
    coinUpgrades.zPosition = 1
    coinUpgrades.name = "Coin"
    self.addChild(coinUpgrades)


    missileUpgrades.position = CGPoint(x: self.size.width*0.8, y: self.size.height*0.8)
    missileUpgrades.zPosition = 1
    missileUpgrades.name = "missile"
    self.addChild(missileUpgrades)


    /*let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
    coinLabel.text = "Coins: \(coinNumber)"
    coinLabel.fontSize = 100
    coinLabel.fontColor = SKColor.black
    coinLabel.zPosition = 1
    coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
    self.addChild(coinLabel) */



}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    for touch: AnyObject in touches {

        let pointOfTouch = touch.location(in: self)
        let nodeITapped = atPoint(pointOfTouch)

        if nodeITapped.name == "Main Menu" {


            let sceneToMoveTo = MainMenuScene(size: self.size)
            sceneToMoveTo.scaleMode = self.scaleMode
            let myTrasition = SKTransition.fade(withDuration: 0.5)
            self.view!.presentScene(sceneToMoveTo, transition: myTrasition)
        }

            if nodeITapped.name == "Life" {
                if coinNumber >= 50 {
                lifeNumber += 1
                coinNumber -= 50
                defaults.set(coinNumber, forKey: "coinNumberSaved")
                defaults.set(lifeNumber, forKey: "lifeNumberSaved")

                    coinLabel.text = "Coins: \(coinNumber)"

                }
        }

                if nodeITapped.name == "Coin" {
                    if coinNumber >= 600 {
                        coinNumber -= 600
                        defaults.set(coinNumber, forKey: "coinNumberSaved")
                        coinUpgrade = 1
                        defaults.set(coinUpgrade, forKey: "coinUpgradeSaved")
                        coinLabel.text = "Coins: \(coinNumber)"

                    }
        }
        if nodeITapped.name == "missile" {
            if missileUpgrade == 2 {
                missileUpgrade = 1
                defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
                if missileUpgrade == 1 {
                    missileUpgrade = 2
                    defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
                }
            }
            else if missileUpgrade == 0 || coinNumber >= 1400 {
                coinNumber -= 1400
                defaults.set(coinNumber, forKey: "coinNumberSaved")
                missileUpgrade = 2
                defaults.set(missileUpgrade, forKey: "missileUpgradeSaved")
                coinLabel.text = "Coins: \(coinNumber)"
                missileUpgrades = SKSpriteNode(imageNamed: "missile")



            }


        }
    }


}

}
  • Please only post relevant code. Seeing this much code in a question pushes people away from reading the question as it becomes a hassle – Nik Oct 04 '16 at 23:14

1 Answers1

1

There are a few ways to do this:

1) Create a Boolean variable that switches to true when the condition is met. Then in the update function, you can constantly check for if the condition is met:

var conditionIsMet = false

// Your code
//
//...

override func update(_ currentTime: CFTimeInterval) {

        if conditionIsMet {

            // Change your texture here

       }

}

2) Where you get the trigger for when you want to change the texture, you can simply change the texture as part of the code/function that runs. That means that if you are trying to do this on a touch, you would add the code to change the texture there.

FYI: To change the texture just do something like this:

node.texture = SKTexture(imageNamed: "image name goes here")
Nik
  • 1,664
  • 2
  • 14
  • 27