| medvedya2 |
14.04.2010 04:50 |
Помогите оптимизировать код: Перемещение обьекта обходя другие объекты
Делаю игру, цель игрока убить как можно больше объектов, объекты гоняться за игроком и его "кусают", на данном этапе уже при 200 объектов игра очень тормозит.
Много ресурсов уходит на перемещение объекта(даже если перемещать без расчетов), и чуть больше уходит на проверку пересечения.
Класс для этих объектов:
Код AS3:
public class Zomb extends MovieClip
{
private var timer:Timer = new Timer(40);
public var ple:Pl;//ссылка на игрока
public var hp:int = 100;
public var rad:int = 25;
public var speed:Number = 2;
public var yron:int = 1
private var starthp:int;
public var zombs:Array;//ссылки на другие оъекты
public function Zomb()
{
}
public function start()
{
starthp = hp;
timer.addEventListener(TimerEvent.TIMER, ontimer);
timer.start();
}
public function end()
{
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, ontimer);
zombs = null;
ple = null;
}
private function ontimer(e:TimerEvent):void
{
var nextposX:Number;
var nextposY:Number;
var nextposX90:Number;
var nextposY90:Number;
var peresechenieP:Boolean;
var peresechenie90P:Boolean;
var p:Point = new Point(ple.x, ple.y);
var pz:Point = new Point(this.x, this.y);
this.rotation = (Math.atan2(this.y - ple.y, this.x - ple.x)) / Math.PI * 180;
nextposX = this.x - Math.cos(this.rotation / 180 * Math.PI) * speed;
nextposY = this.y - Math.sin(this.rotation / 180 * Math.PI) * speed;
peresechenieP = peresechenie(nextposX, nextposY);
if((peresechenieP==false)&&((Point.distance(p, pz) >= (rad+ple.rad)) )){
this.x = nextposX;
this.y = nextposY;
}
if (peresechenieP == true)
{
nextposX90 = this.x - Math.cos((this.rotation + 90) / 180 * Math.PI) * speed;
nextposY90 = this.y - Math.sin((this.rotation +90) / 180 * Math.PI) * speed;
peresechenie90P = peresechenie(nextposX90, nextposY90);
if(peresechenie90P==false)
{
this.x = nextposX90;
this.y = nextposY90;
} else
{
nextposX90 = this.x - Math.cos((this.rotation - 90) / 180 * Math.PI) * speed;
nextposY90 = this.y - Math.sin((this.rotation -90) / 180 * Math.PI) * speed;
peresechenie90P = peresechenie(nextposX90, nextposY90);
if (peresechenie90P == false)
{
this.x = nextposX90;
this.y = nextposY90;
}
}
}
if (Point.distance(p, pz) < (rad+ple.rad)) { ple.hp -= yron; };
ZombMcHp.height = hp / starthp * 30;
}
private function peresechenie(px,py:Number):Boolean
{
var i:int;
var ind:int = zombs.indexOf(this);
var iap:Point = new Point(px,py);
var onp:Point = new Point;
for (i = 0; i < zombs.length; i++)
{
if (i != ind)
{
onp.x = zombs[i].x;
onp.y = zombs[i].y;
if (Point.distance(onp, iap) <= (rad + zombs[i].rad)+speed) {
return(true);
}
}
}
return(false);
}
}
Помогите оптимизировать.
Я Flash начал изучать пару недель назад. Может я что то не правильно делаю?
|