Papa De Beau
Papa De Beau

Reputation: 3828

as3 movie clip dynamic name from array is null

I am using the code below and when I "trace(TheVar);" it is coming out null. Anyone know why?

var TheVar:MovieClip; 
var myArray:Array = ["TheContent", "TheContent2"];

function RandomM()
{
    trace(myArray.length);
    var r = Math.round(Math.random() * myArray.length);
    trace(myArray[r]);
    var TheVar:MovieClip =  myArray[r] as MovieClip;
    trace(TheVar);
}

RandomM();

Update:

I am putting the full code to show what I am trying to do

this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;

var TheContent:Loader = new Loader();
var TheContent2:Loader = new Loader();

function Load1()
{
    TheContent.load(new URLRequest("1.png"));
    TheContent.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadContent);

    function LoadContent(e:Event)
    {
        addChild(TheContent);
        TheContent.width = ScreenX;
        TheContent.height = ScreenY;
        Load2()
    }
}

function Load2()
{
    TheContent2.load(new URLRequest("2.png"));
    TheContent2.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadContent);

    function LoadContent(e:Event)
    {
        trace("Jesus");
        addChild(TheContent2);
        TheContent2.width = ScreenX;
        TheContent2.height = ScreenY;
        RandomM();
    }
}


In the function RandomM() I want to change the Loader "TheVar" to one of these Arrays, where are also Loaders. I want to ultimately create a tween that will change between the loaders according to the swipe of a user.

var TheVar:MovieClip; 
var myArray:Array = [TheContent, "TheContent2"];

function RandomM()
{
    trace(myArray.length);
    var r = Math.round(Math.random() * myArray.length);
    trace(myArray[r]);
    var Base:Class = getDefinitionByName(myArray[r]) as Class;
    var TheVar:MovieClip = new Base();
}

Load1();



Multitouch.inputMode = MultitouchInputMode.GESTURE;

stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler);

function fl_SwipeHandler(event:TransformGestureEvent):void
{
    switch(event.offsetX)
    {
        // swiped right
        case 1:
        {
            // Start your custom code
            // This example code moves the selected object 200 pixels to the right.
            TheVar.x += 200;
            // End your custom code

            break;
        }
        // swiped left
        case -1:
        {
            // Start your custom code
            // This example code moves the selected object 200 pixels to the left.
            TheVar.x -= 200;
            // End your custom code

            break;
        }
    }

    switch(event.offsetY)
    {
        // swiped down
        case 1:
        {
            // Start your custom code
            // This example code moves the selected object 200 pixels down.
            TheVar.y += 200;
            // End your custom code
            break;
        }
        // swiped up
        case -1:
        {
            // Start your custom code
            // This example code moves the selected object 200 pixels up.
            TheVar.y -= 200;
            // End your custom code
            break;
        }
    }
}

ADDING UPDATES TO CLARIFY:

I have a LOADER named TheContent: here is the code:

var TheContent:Loader = new Loader();

I want to create SEVERAL "Loaders" that have images and SWF content. Then I want to move them off screen using ONE TWEEN but have the tween by dynamic so it will move different LOADERS according to what VAR I make the MovieClip in that TWEEN. To do this I will need to make the movie for the TWEEN... CHANGEABLE. I thought maybe the best way to do this is to create an ARRAY with all the NAMES of the LOADERS and then call them randomly and move the content in the TWEEN. Therfore I made an ARRAY:

var myArray:Array = ["TheContent", "TheContent2"];

But I simply can not get the random var out of the function RandomM(); So I can use it in a TWEEN later. That Loader is called "TheVar";

function RandomM()
{

trace(myArray.length);
var r = Math.round(Math.random() * (myArray.length -1) );


var theMC:String = myArray[r];
TheVar=this[theMC] as MovieClip;

trace(TheVar); //// TO BE USED LATER IN A TWEEN. 
///   IT WILL BE ONE TWEEN BUT WILL ANIMATE DIFFERENT MOVIES

}

I hope that makes sense.

Upvotes: 1

Views: 1501

Answers (3)

Marty
Marty

Reputation: 39456

Based on your edits, here's an answer that hopefully covers your problem.

Firstly like I mentioned, your array contains strings and not the loaders themselves. You want to do this:

var myArray:Array = [ TheContent, TheContent2 ];

Notice the lack of " wrapping each.

To select a random loader and assign it to TheVar, do this:

var TheVar:Loader = Loader( myArray[int(Math.random() * myArray.length)] );

You can now work with TheVar as expected.

Upvotes: 1

The_asMan
The_asMan

Reputation: 6402

your issue is with this line

var r = Math.round(Math.random() * myArray.length);

random will give you a number from 0 to 1

myArray.length //will give (in your example) 2

lets say random gives you a value of .75
.75 * 2 = 1.5
When you use the round function it will round to the nearest whole number in this case 2(1.5 and up are rounded up)
When you have a 2 it is out of your array index which should give you a null.
What you need need to do is

var r = Math.round(Math.random() * (myArray.length -1) );

Then you need to access the correct element and get the name of the movie clip

var mcName:String = myArray[r]

Then access to the movie clip itself

var THEVAR:MovieClip = this[mcName];
trace(THEVAR);

Upvotes: 1

Marty
Marty

Reputation: 39456

"TheContent" and "TheContent2" are strings, not MovieClips.

If these are instance names of MovieClips on the stage, you need to use something like this:

var myArray:Array = [this["TheContent"], this["TheContent2"]);

Or more simply:

var myArray:Array = [TheContent, TheContent2];

Assuming that you're working from the timeline.


If TheContent and TheContent2 are classes that you have, you can use getDefinitionByName() to create instance of those classes from the strings you have. Example:

var Base:Class = getDefinitionByName("TheContent") as Class;
var TheVar:MovieClip = new Base();

addChild(TheVar);

Upvotes: 3

Related Questions