2016-06-20 1 views
1

버튼을 만들고 싶습니다. 버튼을 누르면 플레이어의 색상이 버튼의 같은 색상으로 바뀝니다.버튼을 눌러 플레이어 색상을 변경하는 방법은 무엇입니까?

문제는 내가 화면을 누를 때이 작업을한다는 것을, 그리고 난 단지 (이 경우 다른 SKSpriteNode에서) 플레이어 버튼을 눌러

class GameScene: SKScene, SKPhysicsContactDelegate { 

    var circuloPrincipal = SKSpriteNode(imageNamed: "circulo") 

    var colorVerde: UIColor = UIColor(red: 0.3, green: 0.7, blue: 0.5, alpha: 0.9) 

    var circuloVerde = SKSpriteNode(imageNamed: "fondoBaseBlanco.png") 




for touch in touches { 
      let location = circuloVerde 

      let circuloVer = circuloVerde 

      circuloVerde.position = circuloVerde.position 

     circuloPrincipal.color = colorVerde 

     //circulo principal = player 
     //color verde = green color 
     //circulo verde = button 

     } 
} 
+0

이 질문에, 당신은 몇 가지를 너무 광범위 넣을 수있다하여 코드, UIKit 또는 Sprite-kit 라이브러리, 또는 둘 모두에 대한 솔루션을 원하는지 확실하지 않습니다. –

+0

내 질문을 편집했습니다. –

+0

이 "버튼"을 어디에 추가합니까? UIButton입니까? SKScene 있어요? 좀 더 명확히하십시오 .. –

답변

0

당신이 당신의 버튼이 같은 이름을 추가 생성 :

circuloVerde.name = "button" 

touchesBegan 위임 방법은 수행

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     /* Called when a touch begins */ 

     let touch = touches.first 
     let positionInScene = touch!.locationInNode(self) 

     let touchedNode = self.nodeAtPoint(positionInScene) 

     if let name = touchedNode.name 
     { 
      print("name : \(name)") 
      if name == "button" { 
       //do my stuff 
       circuloPrincipal.color = colorVerde 
       // or you can do also 
       circuloPrincipal.color = circuloVerde.color 
      } 
     } 
} 
+1

고맙다. 알레산드로가 도와 줘서 고마워! 그것은 효과가있다! –

1

이 색상을 얻으려면 경우 조치를 재생하려면 버튼을 누르면됩니다.

-(IBAction)buttonTapped:(id)sender{ 
    UIButton *btn = (UIButton*)sender; 
    UIColor *btnColor = btn.backgroundColor; 
// Now you can set this btnColor as the color of your player. 
} 
+0

감사합니다! 나는 그것을 시험해 본다 –

관련 문제