让用户选择按钮有哪个图像?

Letting user chose which image a button has?

我是编码新手,我创建了一个主游戏中带有黑色按钮的游戏 class。我想创建一个设置 class,用户可以在其中单击蓝色按钮以更改主 class 中的黑色按钮或单击红色按钮以更改主 class 中的黑色按钮】 变红。

这里是主要的class:

class Mainclass: SKScene{

 var gameButton = SKSpriteNode(imageNamed: "blackDot") //current button image
}

这里是设置class:

class Settings: SKScene {

override func didMove(to view: SKView) {

self.backgroundColor = SKColor.white

let blueButton = SKSpriteNode(imageNamed: "blueDot") //button to select 
blueButton.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.8)
blueButton.setScale(0.53)
self.addChild(blueButton)

let redButton = SKSpriteNode(imageNamed: "redDot") //button to select 
redButton.position = CGPoint(x: self.size.width * 0.4, y: self.size.height * 0.8)
redButton.setScale(0.53)
self.addChild(redButton)
}

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

for touch in touches{

    let locationUser = touch.location(in: self)

    if atPoint(locationUser) == blueButton {

    //Change the button image in the Mainclass from black to blue if user tap on blueButton

    }

    if atPoint(locationUser) == redButton {

    //Change the button image in the Mainclass from black to red if user tap on redButton

    }
  }
}
}

所以我们有两个场景,名为 GameScene.swiftSettingsScene.swift,相应的 .sks 文件名为 GameScene.sksSettingsScene.sks。我们还有三张图片,名为 default_buttonpurple_buttonblue_button(存储在我们的 .xcassets 文件夹中)。

这里是 SettingsScene.swift:

import SpriteKit

class SettingsScene: SKScene {

    var purpleButton = SKSpriteNode.init(imageNamed: "purple_button")
    var blueButton = SKSpriteNode.init(imageNamed: "blue_button")

    override func didMove(to view: SKView) {
        backgroundColor = .purple

    }

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


        if let location = touches.first?.location(in: self) {

            if purpleButton.contains(location) {
                UserDefaults.standard.set("purple_button", forKey: "button")
            }else if blueButton.contains(location){

                UserDefaults.standard.set("blue_button", forKey: "button")
            }else{

                if let mainScene = GameScene(fileNamed:"GameScene") {

                    //transition to Settings scene
                    self.view?.presentScene(mainScene)
                }
            }
        }
    }
}

这个场景有两个按钮,当用户点击其中一个按钮时,我们会通过在用户默认值中存储适当的图像名称来记住点击的是哪个按钮,例如:

 UserDefaults.standard.set("purple_button", forKey: "button")

稍后我们将从持久存储中读取(在 GameScene didMoveTo:view 方法中)并使用在那里找到的内容。

此外,当用户关闭应用程序时,用户默认值不会被删除,因此用户下次启动应用程序时,他将保存他的设置并准备好使用。

因此,在从 Settings 场景中选择其中一个按钮并转换到 GameScene 之后,您会将按钮设置为您之前选择的任何内容。这是 GameScene:

import SpriteKit


class GameScene: SKScene {

    let button = SKSpriteNode.init(imageNamed: "default_button")
    override  func didMove(to view: SKView)
    {
        if let buttonImageName = UserDefaults.standard.string(forKey: "button") {

            //set what is found in settings
            button.texture = SKTexture.init(imageNamed: buttonImageName)
        }
        backgroundColor = .white
        addChild(button)
    }

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


        if let location = touches.first?.location(in: self) {

            if button.contains(location) {

            }else{
                if let settingScene = SettingsScene(fileNamed:"SettingsScene") {

                    //transition to Settings scene
                    self.view?.presentScene(settingScene)
                }
            }
        }
    }
}