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

victorkhugo
  • 131
  • 1
  • 3
  • 12

6 Answers6

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);
Marty
  • 39,033
  • 19
  • 93
  • 162
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

francis
  • 5,889
  • 3
  • 27
  • 51
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.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
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);
}
Dom
  • 2,569
  • 1
  • 18
  • 28
0

Change your arrary line to:

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

This should work. :)

Papa De Beau
  • 3,744
  • 18
  • 79
  • 137
-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/

victorkhugo
  • 131
  • 1
  • 3
  • 12