Skip to main content
Participating Frequently
May 18, 2021
Question

Code for show/hide multiple buttons and images

Hi Ya'll, 

Im a total noob and really struggling to get this to work. I've managed to get individual images to show/hide using some of the examples I've found in this in this forum, but can't seem to workout how to translate that into muliple objects. My scenario is this.... 

 

I have 4 buttons (btn_Button1, btn_Button2, btn_Button3 and btn_Button4), I want them to show a  corresponding image (mc_Image1, mc_Image2, mc_Image3 and mc_Image4). btn_Button1 shows mc_Image1 and so on. The image covers most of the stage when showing, so I’d like to be able to hide it again by clicking the image.

 

Additionally, I’d like another image (mc_Overlay) to show when ALL buttons are pressed (this image will appear behind mc_image1, mc_image2 etc) and this will also hide when you click on the image, as described above.

 

Does that make sense? and can anyone help??

 

Many thanks

    This topic is closed to new replies. Start a new post to keep the conversation going.

    13 replies

    kglad
    Community Expert
    Community Expert
    May 18, 2021

    as3 or html5?

    Translate
    Participating Frequently
    May 18, 2021

    html5, sorry should have said. 

    Translate
    kglad
    Community Expert
    Community Expert
    May 28, 2021

    Thank you!

    That's very close although the overlay is not appearing with when you press every button, it only seems to appear when you click on the last box on the right and then doesn't disappear again.

    https://we.tl/t-lvWQGiHyWJ

     

    it wont let me attach for some reason 


    overlay appears when you click each button.  clear your cache, and click the right most button, does overlay appear?

     

    and i didn't code for overlay to disappear because i didn't see that in you "asks".  but if that's what you want add an event listener to it must like the movieclips have:

     

    this.btn_pressedA = [];
    this.btn_num = 4;

    this.mc_Overlay.addEventListener("click",mc_clickF.bind(this));

    this.mc_Overlay.visible = false;


    for(var i=1;i<=this.btn_num;i++){
    this["Button"+i+"_btn"].addEventListener("click",btn_clickF.bind(this));
    this["Image"+i+"_mc"].addEventListener("click",mc_clickF.bind(this));
    this["Image"+i+"_mc"].visible = false;
    }
    function btn_clickF(e){
    var i = e.currentTarget.name.split("Button")[1];
    this["Image"+i+"_mc"].visible = true;
    if(this.btn_pressedA.indexOf(i)==-1){
    this.btn_pressedA.push(i);
    }
    if(this.btn_pressedA.length==this.btn_num){
    this.mc_Overlay.visible = true;}
    }

    function mc_clickF(e){
    e.currentTarget.visible = false;
    }

    Translate