2

Let's say I have an array, each item in the array has a corresponding library item.

I'd like to do something like :

var rando = Math.round(Math.random()*3)
var myArray = new Array ["ball", "wall", "fall"]
var i:myArray[rando] = myArray[rando] new myArray[rando]()
addChild(i)

But, this doesn't work. What's the secret?

Thank You,

Victor Hugo

5
  • "this doesn't work" - how exactly? Errors or unexpected behavior? Commented May 1, 2012 at 23:57
  • First of all, you want to floor the random variable, not round it. var rando = Math.floor(Math.random()*3); Commented May 1, 2012 at 23:57
  • 2
    Look closely at what you're doing - would you expect addChild("ball") to work? Commented May 2, 2012 at 0:01
  • I would expect addChild(i) to work if the previous line was i:ball = ball new ball() Commented May 2, 2012 at 0:05
  • 2
    Look at getDefinitionByName(). Commented May 2, 2012 at 0:07

6 Answers 6

4

Surprised no one mentioned getDefinitionByName() here.

Here's some complete code to get your example working:

var myArray = ["ball", "wall", "fall"];

/**
 * Creates a random instance based on an input array containing class names as Strings.
 * @param The input array containing aforementioned Strings.
 * @return The newly created instance.
 */
function createRandom(typeArray:Array):*
{
    // Select random String from typeArray.
    var selection:String = typeArray[ int(Math.random() * typeArray.length) ];

    // Create instance of relevant class.
    var Type:Class = getDefinitionByName(selection) as Class;

    // Return created instance.
    return new Type();
}


// Randomly create and add instance.
var instance:DisplayObject = createRandom(myArray);
addChild(instance);
Sign up to request clarification or add additional context in comments.

Comments

1

Ok so there are a bunch of problems with this.

A large one being var i:myArray[rando] = myArray[rando] new myArray[rando]() not really too sure what you're trying to do here.

Anyway I'm going to assume ball, wall and fall are instance names of MovieClips you have in your library. I think you're going to want something like this

 var rando:int = Math.floor(Math.random()*3); //As the comments point out this should give you a random 
//int between 0 and 2, arrays are 0 indexed so this is what we want if we have 3 items

Now for your array, you're current putting strings in there. Flash has no idea what "ball", etc are.

Try something like this

var myArray:Array = new Array [new ball(), new wall(), new fall()]; //this creates a new instance of your library object and stores it in your array

Now to add one of these to your stage:

addChild(myArray[rando]); //this uses the random number to pull one of the items out of your array

What you're trying to do with var i:myArray[rando] doesn't really make sense. There is no type of myArray[rando] this slot should be holding a MovieClip

Comments

0

If you only have a few choices, it's easier to use a switch-case.

switch (rando) {
   case 0:
       i = new ball();
       break;
   case 1:
       i = new wall();
       break;
   case 2:
       i = new fall();
       break;
}
addChild(i);

I suggest you define the variable i as a MovieClip, in which case it can be instantiated as both ball, wall, fall.

Given that ball, wall and fall are in the library exported to actionscript.

Comments

0

Just guessing off your limited information but give this a shot.

private function myFunction():void{
    var rando = Math.round(Math.random()*3);
    var myArray= new Array ["ball", "wall", "fall"];

}
private function generateItem(item:String):void{
    switch(item){
        case "ball" : generateBall(); break;
        case "wall" : generateWall(); break;
        case "fall" : generateFall(); break;
}

private function generateBall():void{
//code to generate ball
addChild(ball);
}

private function generateFall():void{
//code to generate fall
addChild(fall);
}

private function generateWall():void{
//code to generate wall
addChild(wall);
}

Comments

0

Change your arrary line to:

 var myArray = new Array [ball, wall, fall];

This should work. :)

Comments

-1

Marty Wallace gets big praise for steering me down the path of getDefinitionByName(). The example he posted was good, but this example does exactly what I was going for.

http://www.emanueleferonato.com/2011/03/31/understanding-as3-getdefinitionbyname-for-all-eval-maniacs/

1 Comment

I'm really surprised none of the answers covered this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.