我试图弄清楚如何优化我的游戏。我有很多炮塔等射击物理机构。游戏可玩,但可以下降到50或45帧/秒。我发现它与动态添加对象有关。
例如,。当我的炮塔射击时它会运行此代码
func shootBlaster(){
let blaster = Projectile(color: SKColor.cyanColor(), size: CGSize(width: convertNum(3), height: convertNum(3)))
blaster.name = "blaster"
blaster.physicsBody = SKPhysicsBody(rectangleOfSize: blaster.size)
blaster.physicsBody!.categoryBitMask = CategoryEnemyProjectile
blaster.physicsBody!.contactTestBitMask = CategoryShip | CategoryShipShield
blaster.physicsBody!.collisionBitMask = 0
let fireAction = SKAction.runBlock({
let angle = self.turret.base.zRotation + self.zRotation
let velocity = CGPoint(angle: angle) * convertNum(420)
let vector = CGVectorMake(velocity.x, velocity.y)
let blas = blaster.copy() as Projectile
blas.wpnDmg = 10
blas.position = self.gameScene.gameLayer.convertPoint(self.turret.barrelPos, fromNode: self.turret.base)
self.gameScene.gameLayer.addChild(blas)
blas.physicsBody!.velocity = vector
blas.zRotation = blas.physicsBody!.velocity.angle
blas.runAction(SKAction.removeFromParentAfterDelay(1))
})
let recoilAction = SKAction.moveByX(-5, y: 0, duration: 0.08)
let reverseRecoil = recoilAction.reversedAction()
self.turret.barrel.runAction(SKAction.repeatAction(
SKAction.sequence([
recoilAction,
fireAction,
reverseRecoil
])
,count: self.blasterNum))
}
你可以看到我正在添加一个" blaster"每个fireAction块内部。我注意到性能更好的游戏很少在运行时添加孩子。最好预先装好所有东西。在运行时加载资源会对事物造成压力是有道理的,但是替代方案是什么?我是否需要提前将所有内容添加到场景中并以某种方式隐藏并显示它?
答案 0 :(得分:3)
任何类型的射击游戏,你有许多射弹,你想利用“汇集”。创建一个射弹池并重复使用它们,而不是在需要时创建新射弹。