【问题标题】:Letting user chose which image a button has?让用户选择按钮具有的图像?
【发布时间】:2018-04-17 15:46:13
【问题描述】:

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

这里是主类:

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

    }
  }
}
}

【问题讨论】:

  • 所以你想要两个场景,一个是实际游戏,另一个是设置屏幕?如果是这样,您可以使用用户默认值来保存设置(例如,将在游戏场景中使用哪个图像)。你可以试试,如果你还是卡住了,我可以给你举个例子,告诉我。
  • 是的,我想要两个不同的屏幕。一个很好的例子@Whirlwind

标签: swift button sprite-kit imagebutton


【解决方案1】:

所以我们有两个场景,分别称为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)
                }
            }
        }
    }
}

【讨论】:

  • 谢谢,效果很好。有没有办法让第一个按钮总是黑色的(这意味着如果用户首先下载应用程序并按下播放而不更改按钮它将是黑色的)@Whirlwind
  • 应用程序首次启动时,如果默认值中没有保存任何内容,则按钮将具有默认图像。相关线路(来自GameScene):let button = SKSpriteNode.init(imageNamed: "default_button").
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
相关资源
最近更新 更多