2017-11-13 3 views
0

私のシーンはこのようになります:私はSpriteKitを使用していると私は4つのボタンでこのシーンを作成し enter image description hereボタンが選択されている場合は、その周囲に円を描く方法は?

ユーザーがそのいずれかをタップすると、選択したボタンの周囲に黒い丸が表示されます(そこに留まる)。ユーザーが別のボタンを選択すると、前の円は消え、新しいボタンは円になります。

誰でもこれを行う方法を知っていますか?

はここに私のコードです:あなたはこれを達成できるいくつかの方法があります

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch in touches{ 
     let locationUser = touch.location(in: self) 
     if atPoint(locationUser) == blueButton { 
     } 
     if atPoint(locationUser) == purpleButton { 
     } 
     if atPoint(locationUser) == redButton { 
     } 
     if atPoint(locationUser) == orangeButton { 
     } 
    } 
} 

答えて

1

。私は何をしますか(同じオブジェクトを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を使用している場合

また、この質問の範囲は、あなたはまだ、単にカラーサークルの後ろに黒い円を追加し、このメソッドを使用することができます

+0

オルタナティブサブタイトル:OOPがどのようにシンプルなものを巨大なスクリードにするのか。進捗。 – Confused

関連する問題