0
我在star go中脫離屏幕時,無法刪除我的敵人。一段時間後我會遇到性能問題,因爲我的敵人陣列正在不斷增長和增長。如何停止在starling中渲染屏幕精靈?
這是我的代碼,使我的課程結束時,使敵人以及更新功能。
private function makeEnemies():void
{
enemySpawnCounter++;
if (enemySpawnCounter > enemySpawnDelay)
{
enemySpawnCounter = 0;
enemySpawnDelay -= difficultyRate;
difficulty += difficultyRate;
makeEnemy();
}
}
private function makeEnemy():void
{
var i:int;
for (i = 0; i < Math.floor(difficulty); i++)
{
var newEnemy:Enemy = new SnowBall();
newEnemy.x = 925;
newEnemy.y = Math.random() * 375 + 50;
//trace(newEnemy.x); trace(newEnemy.y);
newEnemy.xVel = (-Math.random() * difficulty) - 5;
newEnemy.sinMeter = Math.random() * 10;
newEnemy.bobValue = Math.random() * difficulty;
enemiesLayer.addChild(newEnemy);
enemies.push(newEnemy);
if (newEnemy.x <= 0)
{
enemies.splice(i, 1);
enemiesLayer.removeChild(newEnemy);
}
}
}
private function update():void
{
trace(enemies.length, lasers.length);
for each (var enemy:Enemy in enemies)
{
enemy.update();
}
for each (var laser:Laser in lasers)
{
laser.update();
newHitTest(laser);
}
makeEnemies();
}
這裏是我的敵人類...
package objects
{
import starling.events.Event;
import starling.display.Sprite;
import objects.Laser;
public class Enemy extends Sprite
{
public static const PURGE_EVENT:String = "ENEMY_PURGE";
public var sinMeter:Number;
public var bobValue:Number;
public var status:String;
public var xVel:Number;
public var yVel:Number;
public function Enemy()
{
status = "OK";
bobValue = 0.1;
sinMeter = 0;
xVel = 0;
yVel = 0;
}
public function destroy():void
{
status = "Dead";
visible = false;
}
public function update():void
{
x += xVel;
y += yVel;
if (x < 0)
{
trace("Dispatching Enemy Escaped!");
dispatchEvent(new Event(Laser.PURGE_EVENT, true, false));
}
}
}
}
任何人都知道如何解決這個問題?