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

Can randomize which movieclips react to a button in HTML5?

Explorer ,
Aug 11, 2022 Aug 11, 2022

Copy link to clipboard

Copied

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

TOPICS
Code , How to , Other

Views

146

Translate

Translate

Report

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

Community Expert , Aug 11, 2022 Aug 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[Mat
...

Votes

Translate

Translate
Community Expert ,
Aug 11, 2022 Aug 11, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

Hi JC,

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

D

Votes

Translate

Translate

Report

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
Explorer ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

Nevermind.  Figured it out!  Thanks JC!

Votes

Translate

Translate

Report

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 Expert ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

LATEST

Awesome! You're welcome!

Votes

Translate

Translate

Report

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