如何通过按下按钮更改播放器颜色?

时间:2016-06-20 06:02:41

标签: ios sprite-kit

我想创建一个按钮,如果我按下它,播放器的颜色会改变为按钮的相同颜色。

问题是,当我点击屏幕时它会播放动作,而我只想在播放器按下按钮时播放动作(在这种情况下是另一个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

        }
}

2 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分: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 
           }
        }
}