我无法删除我的敌人,因为他们离开了椋鸟屏幕。一段时间后我就遇到了性能问题因为我的敌人阵列不断增长和成长。
这是我所拥有的代码,它可以在我课程结束时制作敌人以及更新功能。
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));
}
}
}
}
有人知道如何解决这个问题吗?
答案 0 :(得分:1)
检测他们是否正在触摸屏幕,我从未使用过starling但我认为可能存在某种碰撞检测,所以制作一个与屏幕一致的屏幕大小的矩形或表面,然后只绘制与那个表面碰撞的敌人。