2017-11-13 10 views
0

내 장면은 다음과 같습니다 : 나는 SpriteKit을 사용하고 있는데 나는 네 개의 버튼이 장면을 만들어 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

. 내가 무엇을 할 것인가 (그리고 당신이 같은 객체를 가질 때마다 이것을하는 것을 고려해야한다)는 버튼을 서브 클래스 화하는 것이다. 그렇게하면 버튼 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

Alternative Sub-Title : OOP가 어떻게 간단한 것들을 엄청난 양의 눈에 띄게 만듭니다. 진행. – Confused