在SpriteKit

时间:2016-02-07 19:57:21

标签: ios swift uibutton sprite-kit skspritenode

我想知道是否有一种方法可以让UIButton的属性在按下按钮时使按钮变暗,... SKSpiteNode因为SKSpiteNode已经更多自定义,因为我正在使用SpriteKit。我已经看到了另外一个这样的问题,但没有一个答案奏效。以下是我必须创建SKSpriteNode并检测其上的触摸的代码:

import SpriteKit
class StartScene: SKScene {    
var startButton = SKSpriteNode()
override func didMoveToView(view: SKView) {
    startButton = SKSpriteNode(imageNamed: "playButton")
    startButton.size = CGSize(width: 100, height: 100)
    startButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 50)
    self.addChild(startButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)

        if startButton.containsPoint(location){
           // When it has been selected
        }   
    }
}
//...

请求帮助。在此先感谢...安东

1 个答案:

答案 0 :(得分:1)

我总是通过添加代码来触摸开始实现这一点,并触及结束方法。在这些方法中,我只是将sprite颜色设置为黑色,然后更改其颜色混合因子。如果这对您有用,请告诉我!

  //This will hold the object that gets darkened
    var target = SKSpriteNode() 
    //This will keep track if an object is darkened
    var have = false

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var first = touches.first as! UITouch
        var location:CGPoint = first.locationInNode(self)
        touchP = location
        mouse.position = touchP
        var node:SKNode = self.nodeAtPoint(location)
       if let button = node as? SKSpriteNode
       {
          target = button
           have = true
       }
       else
       {
         have = false
       }

       if (have == true)
       {
           target.color = UIColor.blackColor()
           target.colorBlendFactor = 0.2

       }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
            if (havet == true)
            {
                target.color = UIColor.blackColor()
                target.colorBlendFactor = 0
                target = SKSpriteNode()
                have = false

            }

}