我正在用 Swift 开发一个小游戏,但我一直在努力解决一些问题。在这个自定义类中,我使用了 touchesBegan
方法,在这个方法中我想创建一个 SkNode。这是我的代码:
class TouchableSpriteNode : SKSpriteNode
{
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.removeFromParent()
let delay = Int(arc4random_uniform(UInt32(200)))*Int(NSEC_PER_SEC)/100
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
let random2 = arc4random_uniform(UInt32(self.frame.size.width))
let random3 = arc4random_uniform(UInt32(self.frame.size.height))
div.position = CGPoint(x: Int(random2), y: Int(random3))
self.addChild(div)
}
}
}
问题是,我不能让它显示在屏幕上,因为我得到一个错误,说你不能那个 child ,因为它是他自己的 parent (这很复杂),我想知道的是如何我可以说将 child 添加到场景中,而不是添加到这个 skspritenode 中吗? 任何帮助表示赞赏!谢谢
最佳答案
我打算在没有测试的情况下写这个,但它应该可以工作:在你调用 self.removeFromParent()
之前添加行 parent = self.parent
;然后,使用 parent.addChild(div)
而不是 self.addChild(div)
。
这样做成功了吗?
关于ios - 在哪里添加 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32911834/