0

我在删除我的敌人时遇到了麻烦,因为他们在八哥中离开了屏幕。一段时间后我遇到了性能问题,因为我的敌人数组一直在增长。

这是我在课程结束时制造敌人的代码以及更新功能。

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));
        }
    }
}
}

有人知道如何解决这个问题吗?

4

1 回答 1

1

检测他们是否正在触摸屏幕,我从未使用过八哥,但我认为可能会有某种碰撞检测,所以制作一个与屏幕保持一致的屏幕大小的矩形或表面,然后只绘制敌人正在与那个表面相撞。

于 2013-04-18T17:02:52.277 回答