Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

adding random movie clips to the stage with AS

Community Beginner ,
Aug 02, 2014 Aug 02, 2014

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();

  1. symbolArray.push(liblikas00);

trace(symbolArray[0]);

  1. symbolArray.push(liblikas01);

trace(symbolArray[1]);

  1. symbolArray.push(liblikas02);

trace(symbolArray[2]);

  1. timerLeft.addEventListener(TimerEvent.TIMER, butterflyToStage);
  2. 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;

                }

}

TOPICS
ActionScript
367
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 02, 2014 Aug 02, 2014

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

...
Translate
Community Beginner ,
Aug 02, 2014 Aug 02, 2014

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;

                }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2014 Aug 02, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2014 Aug 02, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2014 Aug 02, 2014

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2014 Aug 02, 2014

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2014 Aug 02, 2014

Thanks again Ned, you helped me a lot!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2014 Aug 02, 2014
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines