Reutilizar Objetos - SAMPLE
/**
* Copyright ne_ ( http://wonderfl.net/user/ne_ )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5Lsd
*/
package {
import flash.text.TextField;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.Sprite;
public class FlashTest extends Sprite {
//misseis on game
private var gMissel:Vector.<Missel> = new Vector.<Missel>();;
//misseis "dead"
private var dMissel:Vector.<Missel> = new Vector.<Missel>();
// timer
private var timer:Timer = new Timer(33);
// contador de objetos
private var t:TextField = new TextField();
public function FlashTest() {
timer.addEventListener(TimerEvent.TIMER, animate);
timer.start();
stage.addChild(t);
t.autoSize = "left";
t.border = true;
t.mouseEnabled = false;
t.multiline = true;
}
private function animate(e:TimerEvent):void{
var m:Missel = addMissel();
m.x = mouseX;
m.y = mouseY;
gMissel.push(m);
addChild(m);
for each(m in gMissel){
m.y -= 5;
if(m.y < 0){
killMissel(m);
}
}
m = null;
var gm:int = gMissel.length;
var dm:int = dMissel.length;
t.text = "game Missel = "+gm+"\ndeath Missel = "+dm+"\nTotal = "+ (gm + dm);
gm = dm = null;
}
private function addMissel ():Missel{
if(dMissel.length) return dMissel.pop();
return new Missel();
}
private function killMissel (m:Missel):void{
removeChild(m);
// remover listeners e resetar vars e etc aqui...
m.reset();
gMissel.splice(gMissel.indexOf(m),1);
dMissel.push(m);
}
}
}
import flash.display.Graphics;
import flash.display.Shape;
internal class Missel extends Shape{
public function Missel(){
reset();
}
public function reset():void{
var f:Function = graphics.lineTo;
graphics.beginFill(uint(Math.random()*0xFFFFFF));
graphics.moveTo(0,-15);
f(5,0);
f(-5,0);
f(0,-15);
graphics.endFill();
f = null;
}
}