Skip to main content
Known Participant
Aug 11, 2022 7:43 PM UTC
Answered

Can randomize which movieclips react to a button in HTML5?

Hi all,

I'm trying to help someone figure something out for a project they're working on. 

They have a series of movieclips which they'll make invisible with the classic "this.movieclipname.visible=false" code.  They have a button that's intended to reveal one movieclip at a time.  Is it possible to set it so that the order in which the movieclips are revealed is totally randomized?  So if someone was to play this in a browser several times they'd be seeing the movieclips in different orders as they click through each time?

Hope I'm explaining that clearly enough...

Thanks

D

This topic is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Aug 11, 2022 8:34 PM UTC

Hi.

 

There several ways of achieving this interactivity. One way would be to store the Movie Clip instances in an Array, sort the array and then increment a variable to access each item of the array in each click. Like this:

 

var root = this;
var index = 0;
var mcs = [ root.yourMC0, root.yourMC1, root.yourMC2, root.yourMC3, root.yourMC4 ];

mcs.sort(function(){ return Math.random() - 0.5 });
mcs.forEach(function(mc){ mc.visible = false; });
root.yourRevealButton.on("click", function(){ mcs[Math.min(index++, mcs.length - 1)].visible = true; });

 

 

I hope this helps.

 

Regards,

JC

4 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Aug 11, 2022 8:34 PM UTC

Hi.

 

There several ways of achieving this interactivity. One way would be to store the Movie Clip instances in an Array, sort the array and then increment a variable to access each item of the array in each click. Like this:

 

var root = this;
var index = 0;
var mcs = [ root.yourMC0, root.yourMC1, root.yourMC2, root.yourMC3, root.yourMC4 ];

mcs.sort(function(){ return Math.random() - 0.5 });
mcs.forEach(function(mc){ mc.visible = false; });
root.yourRevealButton.on("click", function(){ mcs[Math.min(index++, mcs.length - 1)].visible = true; });

 

 

I hope this helps.

 

Regards,

JC

Translate
Known Participant
Aug 12, 2022 7:52 PM UTC

Hi JC,

Would I then place that inside of a mouseclick event handler?

D

Translate