触摸时未检测到 Swift/Spritekit SKNode

标签 swift sprite-kit

我用 swift 和 spritekit 制作了一个小游戏,目前正在处理菜单并制作了一个名为 SKButtonNode 的自定义 Button 类作为 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
    }
}

最佳答案

看起来您从未真正运行过按钮的操作。在 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
    }
}

关于触摸时未检测到 Swift/Spritekit SKNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879941/

相关文章:

ios - 从 URL 到 .scn 文件的 SCNReferenceNode 未找到该文件旁边的纹理

animation - Swift:找不到接受提供的参数的 ' ' 的重载

sprite-kit - 呈现 UIView 时 Spritekit FPS 下降

swift - XCTestCases如何实现异步任务依赖?

ios - 使用 Facebook SDK 登录从一个 Storyboard转到另一个 Storyboard

ios - 无法为某些情况创建圆柱体 - Arkit

ios - 将纹理图集拆分为多个纹理图集警告

ios - 没有 Storyboard的 SpriteKit 示例

objective-c - 使用 SpriteKit 的 TextureAtlas 并通过 Xcode 命令行构建时,iOS 应用程序崩溃

swift - 是否可以在运行 SKAction 的过程中更改 SKNode 的位置?