Copy link to clipboard
Copied
Hi,
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();
trace(symbolArray[0]);
trace(symbolArray[1]);
trace(symbolArray[2]);
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;
}
}
Here is a reduced version of the code:
var timerLeft: Timer = new Timer(5000, 10);
var symbolArray:Array = new Array("butterfly00","butterfly01","butterfly02");
timerLeft.addEventListener(TimerEvent.TIMER, butterflyToStage);
timerLeft.start();
function butterflyToStage(event: TimerEvent): void
{
var butterflyClass = symbolArray[Math.floor(Math.random() * symbolArray.length)];
trace("symbol is " + butterflyClass);
var ClassRef:Class = Class(getDefinitionByNa
...Copy link to clipboard
Copied
reposted the code
var timerLeft: Timer = new Timer(5000, 10);
var liblikas00: butterfly00;
var liblikas01: butterfly01;
var liblikas02: butterfly02;
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;
}
}
Copy link to clipboard
Copied
There are ways of reducing and refining your code, but aside from that I do not see the problem you indicate you have when I test the code. When I test it a new symbol appears every 5 seconds as I would expect.
Copy link to clipboard
Copied
Here is a reduced version of the code:
var timerLeft: Timer = new Timer(5000, 10);
var symbolArray:Array = new Array("butterfly00","butterfly01","butterfly02");
timerLeft.addEventListener(TimerEvent.TIMER, butterflyToStage);
timerLeft.start();
function butterflyToStage(event: TimerEvent): void
{
var butterflyClass = symbolArray[Math.floor(Math.random() * symbolArray.length)];
trace("symbol is " + butterflyClass);
var ClassRef:Class = Class(getDefinitionByName(butterflyClass));
var classInstance:* = new ClassRef();
classInstance.x = stage.stageWidth / 2;
classInstance.y = Math.floor(Math.random() * (1 + 350 - 150)) + 150;
addChild(classInstance);
}
If you want to be able to target the objects then you can place them in an array as you add them and target them thru that.
Copy link to clipboard
Copied
Thank you a lot Ned for your efforts! Especially for the short version of the code. I have to study it a bit in order to fully understand it.
I got the long version of the code also working. Do not completely understand why it was doing what it was doing before. But now its working.
Pushed them also into array and removed from the stage when not needed anymore.
I have a question.
At the moment I’m using a timer, which generates a number of units.
But what should I use, when I want the number of units to be unlimited? I want the code to add an item to the stage every 5 seconds as long as the application is running.
Should I just have the number of units like 100,000 or is there some other way to describe it?
Copy link to clipboard
Copied
If you specify the repeat count to be 0 (zero) or do not specify it at all the timer will repeat indefinitely. The repeatCount property is an int and the default value for an int is zero which is why not specifying it is equivalent to specifying it as 0.
So use:
var timerLeft: Timer = new Timer(5000, 0);
or
var timerLeft: Timer = new Timer(5000);
Copy link to clipboard
Copied
Thanks again Ned, you helped me a lot!
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now