0
votes
import flash.display.MovieClip;

var clip1:clip01 = new clip01;
var clip2:clip02 = new clip02;
var clip3:clip03 = new clip03;
var clip4:clip04 = new clip04;
var clip5:clip05 = new clip05;
var files:Array = [clip1,clip2,clip3,clip4,clip5];

function randomizeArray(array:Array):Array
{
    var newArray:Array = new Array();
    while (array.length > 0)
    newArray.push(array.splice(Math.floor
    (Math.random()*array.length), 1));
    return newArray;
}

var RandomArray:Array = randomizeArray(files);

 trace(RandomArray[0]);
 trace(clip1);

 //var c:MovieClip = MovieClip(RandomArray[0]);
 //addChild(c); not working :(

 addChild(RandomArray[0]); // I want something like this!! Here i got err..
 addChild(clip1);

Compiler message:

[object clip03]
[object clip01]
TypeError: Error #1034: Type Coercion failed: cannot convert []@3ea4aee1 to flash.display.DisplayObject.
    at RandomVideo_fla::MainTimeline/frame1()

I know from trace() that I have the object, what could be the problem then? I pass an MC object to addChild(), so i don't know :(

1

1 Answers

0
votes

Your problem is is that what you're referring to in addChild() is actually an array, not a movie clip. Your RandomArray is actually an array of arrays. This is because the splice() method returns an array with the removed elements, as opposed to the one element you're removing.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#splice()

To fix this, you have to refer to the first element of the returned splice array when you want to add it to your new array. Here's the change you need to make:

newArray.push(array.splice(Math.floor
(Math.random()*array.length), 1)[0]);

Notice the [0] after the splice() call.

Hope this helps!