Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使“removeFromParent()”工作多次?_Ios_Swift_Xcode_Removechild_Addchild - Fatal编程技术网

Ios 如何使“removeFromParent()”工作多次?

Ios 如何使“removeFromParent()”工作多次?,ios,swift,xcode,removechild,addchild,Ios,Swift,Xcode,Removechild,Addchild,我在游戏场景中随机加载了多个SpriteNode,但实际上是同一个SpriteNode被多次添加。我在touchesEnded中有一个函数,一旦在与SpriteNode相同的位置释放触摸,就会删除SpriteNode。这仅适用于添加的第一个SpriteNode的初始SpriteNode,但不适用于所有其他SpriteNode 我试图将代码if object.containslocation转换成一个while循环,这样它就会一直重复。那也没用 var object = SKSpriteNode(

我在游戏场景中随机加载了多个SpriteNode,但实际上是同一个SpriteNode被多次添加。我在touchesEnded中有一个函数,一旦在与SpriteNode相同的位置释放触摸,就会删除SpriteNode。这仅适用于添加的第一个SpriteNode的初始SpriteNode,但不适用于所有其他SpriteNode

我试图将代码if object.containslocation转换成一个while循环,这样它就会一直重复。那也没用

var object = SKSpriteNode()
var objectCount = 0


func spawnObject() {

        object = SKSpriteNode(imageNamed: "image")
        object.position = CGPoint(x: randomX, y: randomY)
        objectCount = objectCount + 1
        self.addChild(object)

}


while objectCount < 10 {

        spawnObject()

}


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        for t in touches {

            let location = t.location(in: self)

            if object.contains(location) {
                object.removeFromParent()
            }

        }

    }

我以为只要我碰一个物体,它就会消失。但是,这只发生在一个对象上,并且与第一个对象的预期效果完全一样,但其他九个对象没有反应。

好的,这是使用数组跟踪衍生对象的基础,以便您可以检查所有对象:

var objectList: [SKSpriteNode] = [] // Create an empty array


func spawnObject() {

    let object = SKSpriteNode(imageNamed: "image")
    object.position = CGPoint(x: randomX, y: randomY)
    self.addChild(object)

    objectList.append(object) // Add this object to our object array

}

while objectList.count < 10 {

spawnObject()

}


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    for t in touches {

        let location = t.location(in: self)

        // Check all objects in the array
        for object in objectList {
            if object.contains(location) {
                object.removeFromParent()
            }
        }
        // Now remove those items from our array
        objectList.removeAll { (object) -> Bool in
            object.contains(location)
        }
    }

}

注意:这并不是最好的方法,尤其是从性能的角度来看,但这足以让人明白这一点。

这实际上只适用于最后一个对象,但您当然不知道这是哪一个。原因是您只保留对单个对象的引用,并用随后创建的每个对象覆盖它。您需要使用数组之类的东西跟踪所有对象,然后检查数组中的每个对象,看看是否需要删除。谢谢您提供的信息。你能想出任何方法来避免使用数组吗,因为我很难正确使用数组。假设你在SKNode中,那么你可以使用touch.locationInNodeself获取当前坐标中的触摸位置,然后使用self.nodesaPointLocation获取该位置所有节点的数组。当然,您将返回到一个数组,并且将获得所有子节点,而不仅仅是使用spawnObject添加的任何子节点。您能否提供一些代码,说明如何使用上面的代码实现数组?因为我不知道如何将其转换为数组或集合。