触摸时未检测到Swift / Spritekit SKNode

时间:2016-06-17 11:01:45

标签: swift sprite-kit

我使用swift和spritekit制作了一个小游戏,我正在编写菜单,并将一个名为SKButtonNode的自定义Buttonclass作为一个Button,它是SKNode的子类。当我点击手机上的按钮时,即使我使用" userInteractionEnabled = true"也没有任何反应。那么为什么我看不到" test2"当我触摸一个按钮但只看到它时,如果我按下一个按钮?

class SKButtonNode: SKNode {
    var defaultButton: SKSpriteNode
    var activeButton: SKSpriteNode
    var action: () -> Void

    init(defaultButtonImage: String, activeButtonImage: String, buttonAction:    () -> Void) {
        defaultButton = SKSpriteNode(imageNamed: defaultButtonImage)
        activeButton = SKSpriteNode(imageNamed: activeButtonImage)
        activeButton.hidden = true
        action = buttonAction

        super.init()

        userInteractionEnabled = true
        addChild(defaultButton)
        addChild(activeButton)
    }

class MenuScene: SKScene, SKPhysicsContactDelegate {

var startGameButton: SKButtonNode!
var optionsGameButton: SKButtonNode!
var exitGameButton: SKButtonNode!


// Update time
var lastUpdateTimeInterval: NSTimeInterval = 0


override func didMoveToView(view: SKView) {
    // Setup physics world's contact delegate
    physicsWorld.contactDelegate = self
    let startGameButton = SKButtonNode(defaultButtonImage: "blue_button_up", activeButtonImage: "blue_button_down" , buttonAction: startGame)

    startGameButton.position = CGPoint(x: 640, y: 330)
    startGameButton.activeButton.size = CGSize(width: 360, height: 95)
    startGameButton.defaultButton.size = CGSize(width: 360, height: 95)
    startGameButton.name = "startGameButton"


    let startOptionsButton = SKButtonNode(defaultButtonImage: "blue_button_up", activeButtonImage: "blue_button_down" , buttonAction: startGame)

    startOptionsButton.position = CGPoint(x: 640, y: 210)
    startOptionsButton.activeButton.size = CGSize(width: 360, height: 95)
    startOptionsButton.defaultButton.size = CGSize(width: 360, height: 95)
    startOptionsButton.name = "startOptionsButton"

    let exitGameButton = SKButtonNode(defaultButtonImage: "blue_button_up", activeButtonImage: "blue_button_down" , buttonAction: startGame)

    exitGameButton.position = CGPoint(x: 640, y: 90)
    exitGameButton.activeButton.size = CGSize(width: 360, height: 95)
    exitGameButton.defaultButton.size = CGSize(width: 360, height: 95)
    exitGameButton.name = "exitGameButton"

    addChild(startGameButton)
    addChild(startOptionsButton)
    addChild(exitGameButton)


}
func exitGame() {

}

func startOptions(){

}

func startGame() {
    print("test")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let location = touches.first!.locationInNode(self)
    let node = self.nodeAtPoint(location)
    print("test2")
    if (node.name == "startGameButton") {
        let currentNode = node as! SKButtonNode
        currentNode.activeButton.hidden = false
        currentNode.defaultButton.hidden = true
    }

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let location = touches.first!.locationInNode(self)
    let node = self.nodeAtPoint(location)
    if (node.name == "startGameButton") {
        let currentNode = node as! SKButtonNode
        if currentNode.defaultButton.containsPoint(location) {
            currentNode.activeButton.hidden = false
            currentNode.defaultButton.hidden = true
        } else {
            currentNode.activeButton.hidden = true
            currentNode.defaultButton.hidden = false
        }
    }

}


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let location = touches.first!.locationInNode(self)
    let node = self.nodeAtPoint(location)
    print("test3")
    if (node.name == "startGameButton") {
        let currentNode = node as! SKButtonNode
        if currentNode.defaultButton.containsPoint(location) {
            startGame()
        }
        currentNode.activeButton.hidden = true
        currentNode.defaultButton.hidden = false
    }
}

1 个答案:

答案 0 :(得分:1)

看起来你实际上从未真正按下按钮的动作。在touchesBegan按下按钮时,您所做的只是将.hidden属性设置为truefalse。尝试这样的事情:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let location = touches.first!.locationInNode(self)
    let node = self.nodeAtPoint(location)

    print("test2")

    if (node.name == "startGameButton") {
        let currentNode = node as! SKButtonNode

        self.runAction(currentNode.action)

        currentNode.activeButton.hidden = false
        currentNode.defaultButton.hidden = true
    }
}