Skip to main content
Inspiring
August 13, 2018
Answered

Play Random Movie Clips by main timeline progress

  • August 13, 2018
  • 3 replies
  • 2294 views

I have 48 movie clips numbered as follows:

Music_mc1, Music_mc2, Music_mc3, etc. to Music_mc48

I need a code that will trigger a random movie clip at each location where the code resides on the main timeline.

Here is what I have:

var SelectMC: Number = 0; {

function GetMC(): void {

var SelRandom: Number = Math.random();

SelectMC = Math.round(SelRandom * 47 + 1);

//trace (SelectMC);

}

GetMC();

var Holder: Object = new Object();

Holder = ("Music_mc" + String(SelectMC));

trace(Holder);

this[Holder].gotoAndPlay(2);

}

By the Output panel, the random MCs are selected correctly. But the swf stops at the first instance of the code (frame 222), and I get this error message:

TypeError: Error #1010: A term is undefined and has no properties.

at Unit19_fla::MainTimeline/frame222()

All help is appreciated!

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

I'm not sure exactly how you setup the objects in the timeline. So please check if this is what you want.

You can call the function when a button is clicked as well.

AS3 code:

Frame 1:

var total:uint = 3;

function playRandomMovieClip():void

{

     this["Music_mc" + Math.round(1 + Math.random() * (total - 1))].gotoAndPlay(1);

}

Frame 222:

playRandomMovieClip();

FLA download:

animate_cc_as3_play_random_movie_clip.zip - Google Drive

Regards,

JC

3 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
August 17, 2018

Hi.

I'm not sure exactly how you setup the objects in the timeline. So please check if this is what you want.

You can call the function when a button is clicked as well.

AS3 code:

Frame 1:

var total:uint = 3;

function playRandomMovieClip():void

{

     this["Music_mc" + Math.round(1 + Math.random() * (total - 1))].gotoAndPlay(1);

}

Frame 222:

playRandomMovieClip();

FLA download:

animate_cc_as3_play_random_movie_clip.zip - Google Drive

Regards,

JC

BuzzyTooAuthor
Inspiring
August 18, 2018

Thanks for the simpler code. What additions to that simple code would eliminate immediate duplications? I don't mind ABA, but I would prefer to avoid AAB or ABB, etc.

I am now having problems with another set of movieclips (13 of them) that run in sync with the main timeline because they have to precisely hide/reveal elements on the main timeline at exact points in the timing/progress of the printed music. I have used buttons to make them visible or invisible as needed, but they are always running to ensure synchronization with the main timeline. That is an issue that may be requiring lots of memory.

I am working through that issue. Before I am finished, I am almost certain that I will be back to this string for one problem or the other.

Thanks again - to JC and Ned.

JoãoCésar17023019
Community Expert
Community Expert
August 18, 2018

For the randomization, you could store the possible indexes in a array and then randomize it every time all the values are picked.

if (!this.started)

{

    var total:uint = 3;

    var indexes:Array = [];

    var count:uint = 0;

    for (var i:int = 0; i < total; i++)

          indexes = i + 1;

    randomizeArray();

    this.started = true;

}

function playRandomMovieClip():void

{

    var index:uint = ++count % total;

    this["Music_mc" + indexes[index]].gotoAndPlay(1);

    if (index == 0)

          randomizeArray();

}

function randomizeArray():void

{

    indexes.sort(function(a:*, b:*):int

    {

         if (Math.random() < 0.5)

              return -1;

         else

              return 1;

    });

}

About the other issues, I'll need to see your FLA.

Regards,

JC

Ned Murphy
Legend
August 17, 2018

I doubt there is a memory issue relative to the buttons not working.  I suspect that the buttons were the next thing in line to be fixed and they finally had a chance to make that known.

Ned Murphy
Legend
August 14, 2018

Where are the mc's located?

BuzzyTooAuthor
Inspiring
August 14, 2018

Thanks, Ned.

The movie clips are on a layer of the timeline. That is a single cell that extends from Frame 222 to the end of the timeline.

Each movie clip is 93 frames long with stop(); on Frame 1. The layer in which the movie clips reside has nothing from frames 1-221, and the movie clips are ln frames 222 to the end of the fla.

Ned Murphy
Legend
August 15, 2018

For the time being, try moving that single cell that holds the movieclips from frame 222 to frame 1 of the main timeline and see if the error persists.