Skip to main content
Known Participant
August 11, 2022
Answered

Can randomize which movieclips react to a button in HTML5?

  • August 11, 2022
  • 1 reply
  • 299 views

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 has been closed for replies.
Correct answer JoãoCésar17023019

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

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
August 11, 2022

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

Known Participant
August 12, 2022

Hi JC,

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

D