SKSpriteKit:如何检测自定义SKSpiteNode“按钮”的“触摸开始”

时间:2018-06-16 03:40:42

标签: ios swift sprite-kit skspritenode touchesbegan

我创建了一个用于在我的应用中创建按钮的类。

Button类的代码如下:

import UIKit
import SpriteKit

class SKButtons: SKSpriteNode {

var sprtButton : SKSpriteNode

    init(image strImageName : String) {

        let texture : SKTexture = SKTexture(imageNamed: strImageName)

        sprtButton = SKSpriteNode(imageNamed: strImageName)

        super.init(texture: texture, color: UIColor.clear, size: texture.size())

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

此按钮用于我的GameScene(继承自SKScene),我想在触摸按钮时检测触摸开始事件。 GameScene的简化代码是这样的:

import SpriteKit
import GameplayKit

class GameScene: SKScene {}
//
// 
    let btnControl1 = SKButtons(image: "button")
    btnControl1.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    btnControl1.size = CGSize(width: 20, height: 20)
    btnControl1.zPosition = 3
    btnControl1.name = "btnControl1"
    self.addChild(btnControl1)
//
//

接触开始起作用,

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

        for t in touches {

            let cgPointTouched = t.location(in: self)
            let skNodeTouched : SKNode = self.atPoint(cgPointTouched)

            switch skNodeTouched.name {

            case "btnControl1":

                print("btnControl1 touched")

            default:
                return

            }   
        }
    }

我在接触开始时设置了一个断点,它没有开火。

那么,我如何让我的触摸开始在GameScene中启动功能,当触摸放置按钮时触发。该按钮是一类SKSpriteNode。

感谢。

0 个答案:

没有答案
相关问题