我的应用程序(Swift 2)在离开场景时不会删除任何节点。最终他们会占用空间并降低帧速率。我的代码可以在https://github.com/Ph0enix0/Loominarty-Confirmed/blob/master/Loominarty%20Confirmed/GameScene.swift找到,谢谢!
答案 0 :(得分:1)
根据您的情况,根据您移动子弹的方式,最简单的解决方案是使用动作序列。像这样:
func SpawnBullets(){
var bullet = SKSpriteNode(color: SKColor.orangeColor(), size: CGSize(width: 10, height: 20))
bullet.zPosition = -5
bullet.position = CGPointMake(player.position.x, player.position.y)
let action = SKAction.moveToY(self.size.height + 30.0, duration: 0.6)
let remove = SKAction.runBlock({bullet.removeFromParent(); println("Bullet removed from scene")})
let sequence = SKAction.sequence([action,remove])
bullet.runAction(sequence)
bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
bullet.physicsBody?.categoryBitMask = physCat.bullet
bullet.physicsBody?.contactTestBitMask = physCat.enemy
bullet.physicsBody?.affectedByGravity = false
bullet.physicsBody?.dynamic = false
self.addChild(bullet)
}
你可能已经意识到了这一点,但是不要害怕提到删除屏幕外节点是一个好习惯,因为添加到节点树的每个节点都会停留在那里并消耗资源,直到删除它为止。