swift - SKSpriteNode 未检测到触摸事件

标签 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")
    }
}

最佳答案

答案是,您需要使用 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)

关于swift - SKSpriteNode 未检测到触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26515315/

相关文章:

swift - 在 Swift 中有选择地从 NSMutableArray 中移除和删除对象

swift & iOS8 : SKLabelNode not showing up

swift - SKSpriteNode - 中断 animateWithTextures 后纹理未恢复到原始大小

ios - Skaction 之后是场景转换

ios - Swift:在 touchesBegan/touchesEnded 中延迟双击

ios - SkSpriteNode没有成员(member)中心

ios - SpriteKit : static background in the scene with moving SKCameraNode

swift - 如果缓存数据已过时,是否可以只从 Firebase 下载数据?

ios - 如何使用 Swift 使用 Firebase 正确观察两个连接的节点

ios - 快速将文本字段传递给第二个 View Controller 中的标签