touchesBegan在SKShapeNode上未获取节点名称

时间:2019-01-17 17:07:18

标签: swift touchesbegan skshapenode

我正在扩展SKShapeNode类,并想要实现一个touchesBegan方法,该方法应返回被触摸节点的名称。将创建此类的多个实例。

但是,不会显示节点名称,而是始终显示(空)。

var menu: ResourceMenu

init(screenSize: CGSize, rectOf: CGSize, cornerRadius: CGFloat, color: UIColor, position: CGPoint, resource: String) {
    self.menu = ResourceMenu(_size: screenSize)

    super.init()

    let rect = CGRect(x: -rectOf.width / 2, y: -rectOf.height / 2, width: rectOf.width, height: rectOf.height)
    self.path = CGPath(roundedRect: rect, cornerWidth: CGFloat(10), cornerHeight: CGFloat(10), transform: nil)

    self.position = CGPoint(x: position.x, y: position.y)
    self.fillColor = color
    self.strokeColor = color
    self.zPosition = 10
    self.isUserInteractionEnabled = true
    self.name = resource

    menu.addChildNodes()
    menu.position = CGPoint(x: -position.x, y: -position.y)

    self.addChild(menu)
}

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

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self)
    let node: SKNode = self.atPoint(location)

    print(node.name)
}

0 个答案:

没有答案