一旦满足条件,如何更改SKSpriteNode图像?

时间:2016-10-04 22:24:24

标签: swift skspritenode

我试图在购买升级时改变我店铺场景中SKSpriteNode的图像,我相信我在变量的正确轨道上,但我认为我做错了。感谢先进的帮助,我已经尝试了一段时间,但没有运气。

//  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")



            }


        }
    }


}

}

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点:

1)创建一个布尔变量,当满足条件时切换为true。然后在更新功能中,您可以不断检查是否满足条件:

var conditionIsMet = false

// Your code
//
//...

override func update(_ currentTime: CFTimeInterval) {

        if conditionIsMet {

            // Change your texture here

       }

}

2)当您想要更改纹理时获得触发器时,您可以简单地将纹理更改为运行的代码/函数的一部分。这意味着,如果您尝试在触摸时执行此操作,则可以添加代码以更改其中的纹理。

仅供参考:要更改纹理,只需执行以下操作:

node.texture = SKTexture(imageNamed: "image name goes here")