1
votes

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.

3
Your first block doesn't seem to be related to your latter additions which is why I got frustrated/confused and stopped providing help. Can you confirm firstly if your array contains STRINGS "TheContent" and "TheContent2" or instances of Loader??Marty
Also may I suggest that var r = Math.round(Math.random() * myArray.length); should be var r:int = Math.random() * myArray.length to avoid going out of bounds.Marty
oK I made some changes. Thanks so much.Papa De Beau
So you have an array of loaders and you want to select a random one? Does that encapsulate the entire issue?Marty
Yes that's what I want to do. Perhaps the code can be entirely rewritten. Maybe I am doing it the hard way. But in short I want to call a random Loader and call a Tween function that I will create later to MOVE that randomly loaded loader accordingly.Papa De Beau

3 Answers

3
votes

"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);
1
votes

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);
1
votes

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.