ios - 在哪里添加 child ?

标签 ios swift sprite-kit

我正在用 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/

相关文章:

ios - NS_Deprecated 等效于 3rd 方框架

ios - 向 SwiftUI TextField 添加不可编辑的后缀

swift - 如何从 UIViewController 到场景

ios - 如何将图像添加到 TableView Controller

ios - 无法在 Nest Thermostat 中存储验证码

ios - UIViewControllerAnimatedTransitioning : multiple calls to interruptibleAnimator?

ios - 节点移动后SKSpriteNode的X、Y坐标不变

ios - 列和行索引比 SKTileMapNode 中的列和行数更高

iphone - 在一个App中使用两个库访问iPhone相机

iphone - 将 HEX NSString 转换为 NSData