2016-06-20 27 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

谢谢!我生病了吧 –