SKSpriteNode未检测到触摸事件

时间:2014-10-22 19:15:26

标签: swift sprite-kit skspritenode

我无法触发touchesBegan功能。当用户触摸节点时,不会调用它。

import SpriteKit

class SimpleButton: SKSpriteNode {
    //called when the user taps the button
    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        println("touches began")
    }

    //called when the user finishes touching the button
    override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
        println("touches ended")
    }

    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
        println("touches moved")
    }
}

1 个答案:

答案 0 :(得分:4)

答案是,您需要使用userInteractionEnabled参数启用触摸。

let imageTexture = SKTexture(imageNamed: "a_button")
let sprite: SimpleButton = SimpleButton(texture: imageTexture)

sprite.userInteractionEnabled = true  // <---  This is required, it defaults to false.

sprite.position = CGPoint(x:CGRectGetMidX(self.frame) - 200, y:CGRectGetMidY(self.frame));
sprite.color = UIColor.blackColor()
sprite.colorBlendFactor = 1.0
self.addChild(sprite)