【发布时间】:2017-11-14 12:05:32
【问题描述】:
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 是否包含我的 GameScene 中的触摸
添加
for touch in touches {
let location = touch.location(in: self)
if MainNode.SubclassNode.contains(location) {
print("Subclass contains touch")
}
}
我的 GameScene 课程或我的 MainNode 课程似乎根本不起作用,即使 SubclassNode.isUserInteractionEnabled = true
如何确定触摸是否在我的子类中?
【问题讨论】:
-
您使用了错误的方法,您想使用
atPoint获取某个节点并检查该节点是否为SubclassNode。另外我建议不要以大写字母开头变量,这会引起混淆。类型应该是大写的,而变量应该是小写的。 -
你能给我举个例子吗?我也给子类起了一个名字,但它仍然不起作用。 (对不起,我通常使用小写的大写字母。这是一些经过编辑的代码,我并没有真正注意)
标签: ios swift sprite-kit touchesbegan