0

Its quite a big task but ill try to explain.

I have an array with a list of 200 strings and I want to be able to randomly select one and add it to the stage using code. I have movieclips exported for actionscript with the same class name as the strings in the array. Also, if it is possible, would I be able to select the strings with predictability such as the first has a 0.7 chance the second a 0.1 etc. Here is what i have currently

var nameList:Array=["Jimmy","Bob","Fred"]

var instance:DisplayObject = createRandom(nameList);
addChild(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();
}

All this throws me this error

ReferenceError: Error #1065: Variable [class Jimmy] is not defined.

Ive searched for other threads similar but none combine the three specific tasks of randomisation, predictability and addChild().

1 Answers1

0

I think that you've got two problems: a language problem and a logic problem. In the .fla connected to your code above, in the Library find each symbol representing a name and write into the 'AS linkage' column for that symbol the associated name -- e.g., 'Bob,' 'Fred' -- just the name, no punctuation.

Now getDefinitionByName() will find your 'Class'

If you put a different graphic into each MovieClip -- say, a piece of fruit or a picture of Bob,Jim, Fred -- and run your program you'll get a random something on stage each time.

That should solve your language problem. But the logic problem is a little harder, no? That's why I pointed you to Mr. Kelly's solution (the first one, which for me is easier to grasp).

Craig
  • 814
  • 1
  • 6
  • 9
  • Ill mark this as right but ive figured out the getdefinition and will come back to the probability later thanks to everyone who commented. – BennettLiam Feb 22 '14 at 21:44