PDA

View Full Version : Pull and display a random MC from an array?


Vathian
10-12-2008, 04:25 AM
var tstArray:Array = [a_mc, b_mc, c_mc];
trace(tstArray);
stage.addEventListener(MouseEvent.CLICK, addletter);
function addletter (MouseEvent):void
{
var t:Number = Math.floor((Math.random()*tstArray.length));
addChild(tstArray[t]);
}


Basically what I'm attempting is to call a random movieclip (all of which have "export for actionscript" ticked) and then display it on the screen.

It currently randomly pulls from the array but I cannot seem to get it to display it on the stage when clicked.

I receive this error:
"TypeError: Error #1034: Type Coercion failed: cannot convert c_mc$ to flash.display.DisplayObject. at Untitled_fla::MainTimeline/addletter()"

It also, when traced what's in the array, presents each as a class:
"[class a_mc],[class b_mc],[class c_mc]"

Am I going the completely wrong direction here?

Thanks in advance for any and all help!

Mazoonist
10-12-2008, 05:54 AM
You're not going in the wrong direction, you just aren't creating objects from your classes. Basically, when you set a movieclip to "export for actionscript" you are creating a custom class that extends the MovieClip class. To display an instance of this class to the stage, you have to use the "new" keyword.

You should follow the naming convention of capitalizing your class names (aka "export" name). So, let's say you had a MovieClip in the library with an export/class name of "Ball" you would make an instance like so:
var ball:Ball = new Ball();
addChild(ball);

So in your example, you would have to instantiate the three movieclips before you index them into an array. In the following, I didn't correct your export name, but you should consider some kind of capitalization so as not to confuse yourself:
//make a new instance of each clip
var a_mc:a_mc = new a_mc();
var b_mc:b_mc = new b_mc();
var c_mc:c_mc = new c_mc();

//store references in an array
var tstArray:Array = [a_mc, b_mc, c_mc];
trace(tstArray);

//I would also suggest using int type for array index
//instead of number. Also, you must provide a variable
//name for the MouseEvent parameter:
stage.addEventListener(MouseEvent.CLICK, addletter);
function addletter(event:MouseEvent):void
{
var t:int = Math.floor((Math.random()*tstArray.length));
addChild(tstArray[t]);
}

Try the above and see if it works for you.

Vathian
10-12-2008, 06:32 AM
Thank you so very much for the prompt response!

And because I hate finding a thread that has the info I want but not the final real life application of said code...here's the finalized version for future search engine results: (with some renaming of the variables and array)


var amov:Amov = new Amov();
var bmov:Bmov = new Bmov();
var cmov:Cmov = new Cmov();
var dmov:Dmov = new Dmov();

var Diff1:Array = [amov, bmov, cmov, dmov];
trace(Diff1);

stage.addEventListener(MouseEvent.CLICK, addDiff1);
function addDiff1(event:MouseEvent):void
{
var randMov:int = Math.floor((Math.random()*Diff1.length));
addChild(Diff1[randMov]);
}