当SKSpriteNode离开SKScene时自动调用removeFromParent()

时间:2022-02-01 19:02:43

It is my understanding that SKSpriteNodes that have been added to the SKScene will linger there until removeFromParent(), or any of the other remove methods are called.

我的理解是,添加到SKScene的SKSpriteNodes将在那里停留,直到removeFromParent()或任何其他remove方法被调用。

I haven't touched SpriteKit for 2 months, and that's what I remember it to be. But for some reason, today when I was programming, I realized something odd; I was looking at my node count and it was not being increased even though I am doing a .repeatActionForever that keeps instantiating a new SKSpriteNode that runs across the screen. There are no remove calls anywhere in the code. Here's the block of code that creates the SKSpriteNode:

我没有接触SpriteKit 2个月,这就是我记得的。但出于某种原因,今天当我编程的时候,我发现了一些奇怪的东西;我正在查看我的节点数,并且它没有被增加,即使我正在执行一个.repeatActionForever,它继续实例化一个在屏幕上运行的新SKSpriteNode。代码中的任何地方都没有删除调用。这是创建SKSpriteNode的代码块:

//function in GameScene
func spawnEnemy() {
    let enemy = SKSpriteNode(imageNamed: "enemy")
    enemy.position = CGPoint(x: size.width + enemy.size.width/2, y: size.height/2)
    let actionMove = SKAction.moveToX(-enemy.size.width/2, duration: 2.0)
    enemy.runAction(actionMove)
}

//inside update()
runAction(SKAction.repeatActionForever(SKAction.runBlock(spawnEnemy))

Here's a gif of what the simulator showed: http://i.imgur.com/c8eyFDE.gif

这是模拟器显示的GIF图形:http://i.imgur.com/c8eyFDE.gif

I was under the impression that the stampede of old lady in dresses would linger off the screen and the node count for continue to build up, but for some reason, the node count stays constant at 42 nodes, as if the nodes that are off screen are automatically removed.

我的印象是衣服中的老太太踩踏事件会在屏幕上徘徊,节点数量会继续增加,但由于某种原因,节点数保持在42个节点不变,就好像屏幕外的节点一样被自动删除。

Again, I have no remove calls anywhere.

同样,我没有删除任何电话。

2 个解决方案

#1


The node count only counts the nodes visible on screen. Therefore you've got a constant 42 nodes on screen and many more off screen.

节点计数仅计算屏幕上可见的节点。因此,屏幕上有42个节点,屏幕外还有更多节点。

To check nothing is being removed though, you could subclass SKSpriteNode like so:

要检查是否正在删除任何内容,您可以像这样子类化SKSpriteNode:

class Enemy : SKSpriteNode {
    override func removeFromParent() {
        super.removeFromParent()
        println("An enemy was removed!")
    }
}

Then in your scene:

然后在你的场景中:

let enemy = Enemy(imageNamed: "enemy")

Now, if an enemy is somehow being removed you'll definitely know.

现在,如果敌人以某种方式被移除,你肯定会知道。

If it turns out the enemies are persisting after they've moved off the screen I would recommend using either of the following to remove the nodes:

如果事实证明敌人在离开屏幕后仍然存在,我建议使用以下任一方法删除节点:

1. Use runAction(_ action: SKAction!, completion block: (() -> Void)!) like so:

1.使用runAction(_ action:SKAction!,completion block:(() - > Void)!)如下:

enemy.runAction(actionMove) { enemy.removeFromParent() }

2. Use SKAction.removeFromParent() and create a sequence of SKActions:

2.使用SKAction.removeFromParent()并创建一系列SKActions:

let actionMove = SKAction.moveToX(-enemy.size.width/2, duration: 2.0)
let remove = SKAction.removeFromParent()

enemy.runAction(SKAction.sequence([actionMove, remove]))

Hope that helps answer you question.

希望有助于回答你的问题。

#2


There may be some bugs in your code:

您的代码中可能存在一些错误:

1.I hadn't see any code about adding a node in the scene. Enemies need to be added to the scene or child of the scene.
2.The method repeadActionForever doesn't need to be called for more than one times. It will run the block inside automatically.

1.我没有看到任何关于在场景中添加节点的代码。需要将敌人添加到场景的场景或孩子中。 2.方法repeadActionForever不需要被调用超过一次。它将自动运行块内部。

Try to fix them and let me know what happens.

尝试解决它们,让我知道会发生什么。

#1


The node count only counts the nodes visible on screen. Therefore you've got a constant 42 nodes on screen and many more off screen.

节点计数仅计算屏幕上可见的节点。因此,屏幕上有42个节点,屏幕外还有更多节点。

To check nothing is being removed though, you could subclass SKSpriteNode like so:

要检查是否正在删除任何内容,您可以像这样子类化SKSpriteNode:

class Enemy : SKSpriteNode {
    override func removeFromParent() {
        super.removeFromParent()
        println("An enemy was removed!")
    }
}

Then in your scene:

然后在你的场景中:

let enemy = Enemy(imageNamed: "enemy")

Now, if an enemy is somehow being removed you'll definitely know.

现在,如果敌人以某种方式被移除,你肯定会知道。

If it turns out the enemies are persisting after they've moved off the screen I would recommend using either of the following to remove the nodes:

如果事实证明敌人在离开屏幕后仍然存在,我建议使用以下任一方法删除节点:

1. Use runAction(_ action: SKAction!, completion block: (() -> Void)!) like so:

1.使用runAction(_ action:SKAction!,completion block:(() - > Void)!)如下:

enemy.runAction(actionMove) { enemy.removeFromParent() }

2. Use SKAction.removeFromParent() and create a sequence of SKActions:

2.使用SKAction.removeFromParent()并创建一系列SKActions:

let actionMove = SKAction.moveToX(-enemy.size.width/2, duration: 2.0)
let remove = SKAction.removeFromParent()

enemy.runAction(SKAction.sequence([actionMove, remove]))

Hope that helps answer you question.

希望有助于回答你的问题。

#2


There may be some bugs in your code:

您的代码中可能存在一些错误:

1.I hadn't see any code about adding a node in the scene. Enemies need to be added to the scene or child of the scene.
2.The method repeadActionForever doesn't need to be called for more than one times. It will run the block inside automatically.

1.我没有看到任何关于在场景中添加节点的代码。需要将敌人添加到场景的场景或孩子中。 2.方法repeadActionForever不需要被调用超过一次。它将自动运行块内部。

Try to fix them and let me know what happens.

尝试解决它们,让我知道会发生什么。