AS3: My open source “push array”.
What does it do? It stores things inside an array. You add to the array by going array.Push(Obj) and pop from the array by doing array.Pop(Obj).
What would I use it for? Lets say you have a space shooter game. Enemies are constantly flying in and out of the game. In order to hit test them properly with bullets and without (much) lag, you would create a push array and push all enemies that exist into the array. Whenever one dies or flies off screen, you pop it from the array.
(This class is not optimized. I’ll probably put up a faster one later that counts the number of objects in the array instead of just going straight to 200. Still, I think it might be helpful.)
package {
import flash.filters.*;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.*;
import flash.events.MouseEvent;
public class pushArray {
var base:Array = new Array(200);
function pushArray() {
for (var i:int=0; i<200; i++) {
base[i]=undefined;
}
}
public function Find(obj:Object):int {
for (var i:int=0; i<200; i++) {
if (base[i]==obj) {
return i;
break;
}
}
return -1;
}
public function Push(obj:Object):int {
for (var i:int=0; i<200; i++) {
if (base[i]==undefined) {
obj.index=i;
base[i]=obj;
return i;
break;
}
}
return -1;
}
public function Get(index:int):Object {
return base[index];
}
public function PopIndex(index:int) {
base[index]=undefined;
}
public function Pop(obj:Object) {
for (var i:int=0; i<200; i++) {
if (base[i]==obj) {
base[i]=undefined;
break;
}
}
}
public function getArray():Array {
var real:Array = new Array();
var indx:int=0;
for (var i:int=0; i<200; i++) {
if (base[i]!=undefined) {
real[indx++]=base[i];
}
}
return real;
}
public function len():int {
var len:int;
for (var i:int=0; i<200; i++) {
if (base[i]!=undefined) {
len++;
}
}
return len;
}
public function Hit(obj:Object):Object {
for (var i:int=0; i<200; i++) {
if (base[i]!=undefined && base[i]!=obj) {
if (obj.hitTestObject(base[i])) {
return base[i];
}
}
}
return false;
}
public function Trace() {
var vals:String = “”;
for (var i:int=0; i<base.length; i++) {
if (base[i]!=undefined) {
vals = vals + ” ” + base[i];
} else {
vals = vals + ” undef”;
}
}
trace(vals);
}
}
}