。私は何をしますか(同じオブジェクトを4つ持っているときはいつでもこれを行うことを検討してください)は、ボタンをサブクラス化することです。そうすれば、サブクラスにボタンisSelected
があるかどうかを示すプロパティを追加し、それに応じてプロパティを変更することができます。あなたのgameSceneで
class Button: SKSpriteNode {
var isSelected = false {
didSet {
if isSelected {
background.isHidden = false
}
else {
background.isHidden = true
}
}
}
//you can design the init to better suit however you want to differentiate between your buttons
init(color: String) {
//create an image of a black circle that you want to use as your border that is slightly larger than your other images
let texture = SKTexture(imageNamed: "blackCircle")
super.init(texture: nil, color: .clear, size: texture.size())
let background = SKSpriteNode(texture: texture)
background.zPosition = 0
addChild(background)
//add your color image here based on passed in parameters
//but make sure that the zPosition is higher than 0
let buttonTexture = SKTexture(imageNamed: color)
let button = SKSpriteNode(texture: buttonTexture)
button.zPosition = 1
addChild(button)
}
}
私は、アレイ内のボタンを格納します
private var blueButton: Button!
private var purpleButton: Button!
private var redButton: Button!
private var orangeButton: Button!
private var buttons = [Button]()
//example of creating your new button
blueButton = Button(color: "blue")
blueButton.position = CGPoint(x: blah, y: blah)
blueButton.zPosition = 1
addChild(blueButton)
buttons.append(blueButton)
//previously selected Button so that you know which one to unselect
private var prevSelectedButton: Button!
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let locationUser = touch.location(in: self)
//unhighlight the last selected button
prevSelectedButton.isSelected = false
if atPoint(locationUser) == blueButton {
blueButton.isSelected = true
prevSelectedButton = blueButton
}
else if atPoint(locationUser) == purpleButton {
purpleButton.isSelected = true
prevSelectedButton = purpleButton
}
else if atPoint(locationUser) == redButton {
redButton.isSelected = true
prevSelectedButton = redButton
}
else if atPoint(locationUser) == orangeButton {
orangeButton.isSelected = true
prevSelectedButton = orangeButton
}
}
}
私は本当に機能をカプセル化するためにButtonクラスの内部のタッチイベントを処理するだろうが、それは外であることは注目に値する
あなたの代わりにSpriteNodesのshapeNodesを使用している場合
また、この質問の範囲は、あなたはまだ、単にカラーサークルの後ろに黒い円を追加し、このメソッドを使用することができます
オルタナティブサブタイトル:OOPがどのようにシンプルなものを巨大なスクリードにするのか。進捗。 – Confused