AS3: Frame Labels
if (mc.currentLabel == “Start”)
I find this really helpful. It goes a bit more towards the ideal fully flexible Flash movie/game that can have its frames adjusted at will.
if (mc.currentLabel == “Start”)
I find this really helpful. It goes a bit more towards the ideal fully flexible Flash movie/game that can have its frames adjusted at will.
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);
}
}
}
As far as I know, there IS NO WAY. No way to perminently delete it from Flash, anyways. The best you can hope for is clearing every existing variable that references to the clip, then going removeChild(clip) and deleting all listeners that listen for the clip (because they keep an instance of the clip).
e.currentTarget
When you add an event listener, and then do something like this:
function listen(e:Event){
e.target.x+=5;
}
That could actually screw you up. For some dumb reason e.target doesn’t actually always return the target movie clip. ?!?! I don’t understand you, Adobe.
The correct way is of course e.currentTaret.x+=5;
getChildIndex()
Weirdly enough, you cant just do
getChildIndex(e.currentTarget);
You have to do… (deep breath)
e.currentTarget.parent.getChildIndex(DisplayObject(e.currentTarget))