adding random movie clips to the stage with AS
rahex Aug 2, 2014 1:29 AMHi,
I need some help with my code.
I have 3 different movie clips.
They are called butterfly00, butterfly01, butterfly02. They are exported for AS.
I need to add them to the stage using AS. Randomly. Using a timer.
I put them into an array and the idea was to pull them out from there, but it doesn't work properly.
At the moment it works correctly for the first 2 butterflies. After that it adds to stage 2 butterflies on the same time and then 3 and 4 and etc. Accumulates.
Maybe someone can comment the code.
Many thanks in advance!
var timerLeft: Timer = new Timer(5000, 10);
var liblikas00: butterfly00;
var liblikas01: butterfly01;
var liblikas02: butterfly02;
//var liblikas: MovieClip;
var symbolArray: Array;
var symbolButterfly: int;
symbolArray = new Array();
- symbolArray.push(liblikas00);
trace(symbolArray[0]);
- symbolArray.push(liblikas01);
trace(symbolArray[1]);
- symbolArray.push(liblikas02);
trace(symbolArray[2]);
- timerLeft.addEventListener(TimerEvent.TIMER, butterflyToStage);
- timerLeft.start();
function butterflyToStage(event: TimerEvent): void
{
symbolButterfly = Math.floor(Math.random() * symbolArray.length);
trace("symbol is " + symbolButterfly);
switch(symbolButterfly)
{
case symbolButterfly = 0:
var liblikas00: butterfly00 = new butterfly00();
liblikas00.x = stage.stageWidth / 2;
liblikas00.y = Math.floor(Math.random() * (1 + 350 - 150)) + 150;
stage.addChild(liblikas00);
break;
case symbolButterfly = 1:
var liblikas01: butterfly01 = new butterfly01();
liblikas01.x = stage.stageWidth / 2;
liblikas01.y = Math.floor(Math.random() * (1 + 350 - 150)) + 150;
stage.addChild(liblikas01);
break;
case symbolButterfly = 2:
var liblikas02: butterfly02 = new butterfly02();
liblikas02.x = stage.stageWidth / 2;
liblikas02.y = Math.floor(Math.random() * (1 + 350 - 150)) + 150;
stage.addChild(liblikas02);
break;
default:
trace("nothing");
break;
}
}


