Skip to main content
Participating Frequently
May 18, 2021
Question

Code for show/hide multiple buttons and images

  • May 18, 2021
  • 1 reply
  • 964 views

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 has been closed for replies.

    1 reply

    kglad
    Community Expert
    Community Expert
    May 18, 2021

    as3 or html5?

    Participating Frequently
    May 18, 2021

    html5, sorry should have said. 

    kglad
    Community Expert
    Community Expert
    May 18, 2021

    this.btn_pressedA = [];
    this.btn_num = 4;
    for(var i=1;i<=this.btn_num;i++){
    this["btn_Button"+i].addEventListener("click",btn_clickF.bind(this));
    this["mc_Image"+i].addEventListener("click",mc_clickF.bind(this));
    this["mc_Image"+i].visible = false;
    this.mc_Overlay.visible = false;
    }
    function btn_clickF(e){
    var i = e.currentTarget.name.split("Button")[1];
    this["mc_Image"+e.currentTarget.name.split("Button")[1]].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;
    }