Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.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 精灵不会被移除-swift_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 精灵不会被移除-swift

Ios 精灵不会被移除-swift,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我正在尝试移除一些精灵,但并非所有精灵都已移除。请检查下面的代码 func removeSquare(squareSprite : SKNode) { for (index, value) in squares.enumerate() { if Int(value.sprite.position.x) == Int(squareSprite.position.x) { for i in 0..

我正在尝试移除一些精灵,但并非所有精灵都已移除。请检查下面的代码

    func removeSquare(squareSprite : SKNode)   {        

       for (index, value) in squares.enumerate()    {

           if Int(value.sprite.position.x) == Int(squareSprite.position.x)  {
              for i in 0..<2  {
                  if index - i >= 0  {
                    squares[index - i].sprite.removeFromParent()
                    squares.removeAtIndex(index - i)
                    print("index - i is \(index - i)")
                    print("squares.count is \(squares.count)")
                  }
              }
           }
       }
func removeSquare(squareSprite:SKNode){
用于(索引,值)的平方。枚举(){
如果Int(value.sprite.position.x)==Int(squareSprite.position.x){
对于0中的i..=0{
正方形[index-i].sprite.removeFromParent()
正方形.删除索引(索引-i)
打印(“索引-i是\(索引-i)”)
打印(“squares.count为\(squares.count)”)
}
}
}
}
i
大于零时,不会删除精灵,否则当它为零时,会删除精灵。
squares
是一个包含方形精灵的数组,该精灵具有类型为
SKSpriteNode
sprite
属性。我也检查了方块的数量,数字也相应减少,但精灵仍然在屏幕上

问题在于,在遍历数组时,您正在减少数组。尝试注释这一行,以检查您的算法是否在原则上正常工作,以及是否删除了正确的精灵:

squares.removeAtIndex(index - i)

如果是,请将算法更改为从数组的末尾开始。听起来,由于您正在进行平方运算,正方形处的索引将发生变化。删除索引(索引-i),使数组随着每次迭代而变小,因此
1处的
i
在下一个循环中变为
0
,您想反向执行
1..>=0
,这样阵列就变小了,而不必更改阵列中对象的索引。

您想删除哪些精灵,然后呢?听起来好像在执行
方块后,方块处的索引会更改。removeAtIndex(index-i)
,因此,在下一个循环中,1处的i变为0,您想反向运行1..>=0,这样它就不会改变。@Knight0fDragon谢谢,这真的很有帮助。我还使用了另一个变量来表示索引。它正在工作。