我在快速访问节点对象的自定义属性时遇到问题。我会试着向你解释。
我创建了一个继承自 SkSpriteNode 的类,并创建了一个调用( Has Gluten )的属性
init(foodName: String, foodNote: String, foodImage: SKTexture, hasGluten: Bool) {
self.foodNote = foodNote
self.foodImage = foodImage
self.hasGluten = hasGluten
self.foodName = foodName
super.init(texture: foodImage, color: UIColor.clear, size: CGSize(width: 40, height: 40))
self.position = CGPoint(x: Int.random(in: 0..<355), y: 600)
self.name = self.foodName
}
直到这里 好的,但是当我将此节点添加到屏幕并激活触摸开始时,我无法访问该类中的属性 HasGluten .. 它说我只能访问本机属性,例如(名称、位置等)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
let touch:UITouch = touches.first!
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if touchedNode.hasGluten == true { callfunction() } else {call another function}
}
这是我想要访问该属性的点。但是当我尝试访问属性 HasGluten 时,它没有给我。
如果有人可以提供帮助,我会很高兴。谢谢
为了访问您的属性,您应该将节点对象强制转换为您的自定义类型:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
let touch:UITouch = touches.first!
let positionInScene = touch.location(in: self)
guard let touchedNode = self.atPoint(positionInScene) as? CustomNode else { return }
if touchedNode.hasGluten == true { callfunction() } else {call another function}
}
而是CustomNode
使用正确的类名。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句