class MainNode:SKNode {
class Box: SKSpriteNode {
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: SKTexture(imageNamed: "testIMG1"), color: UIColor.clear, size: CGSize(width: 50, height: 50))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
var subclassNode: SKSpriteNode!
var box = Box()
convenience init(size: CGSize) {
self.init()
box = Box()
box.position.x = 0
subclassNode = SKSpriteNode(color: UIColor.red, size: CGSize(width: 40, height: 40))
subclassNode.position.x = 0
subclassNode.zPosition = 6
self.addChild(subclassNode)
}
}
在我的课堂上MainNode
我试图测试SKSpriteNode子类Box
是否包含我的游戏场景中的触摸
添加
for touch in touches {
let location = touch.location(in: self)
if MainNode.SubclassNode.contains(location) {
print("Subclass contains touch")
}
}
到我的 GameScene 类或我的 MainNode
类似乎根本不起作用
SubclassNode.isUserInteractionEnabled = true
如何确定触摸是否在我的子类中?
我在KnightOfDragon的更多研究和帮助下弄清楚
了我设置了self.isUserInteractionEnabled = true
,然后给我的子类节点起了一个名字subclassNode.name = "SubclassNode"
然后将这段代码添加到我的MainNode
类中
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = self.atPoint(location)
if let name = touchedNode.name
{
if name == "SubclassNode" {
print("touched Subclass Node")
}
}
}
}