让用户选择按钮所具有的图像?

时间:2017-11-05 10:51:31

标签: swift button sprite-kit imagebutton

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

这是主要课程:

class Mainclass: SKScene{

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

以下是设置类:

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

    }
  }
}
}

1 个答案:

答案 0 :(得分:1)

因此,我们有两个名为GameScene.swiftSettingsScene.swift的场景以及名为.sksGameScene.sks的相应SettingsScene.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)
                }
            }
        }
    }
}