4 Replies Latest reply on Aug 10, 2017 5:22 PM by ReeFiz

    Open and close action with single button

    ReeFiz

      On my HTML5 Canvas stage I have several buttons that perform different actions such as opening an information box, or even a sub-menu. I'm trying to code it so that when a button is clicked it not only opens the relevant action, but it also closes any other actions that were already opened. Furthermore, if the same button is clicked again it will close its own action.

       

      Is there a function that exists to do this? Thanks, appreciate any help - I'm obviously new and trying my hardest to learn it!

       

      My current process for opening is

       

      this.mc_Contact_Box.visible = false;

      this.Contact_Btn.addEventListener("click", fl_ClickToShow_4.bind(this));

       

      function fl_ClickToShow_4()

      {

          this.mc_Contact_Box.visible = true;

      }

        • 1. Re: Open and close action with single button
          RandomlyFish Level 3

          Here's one way you could write it:

           

          // Store the last opened window
          var lastOpened = null;
          
          // Function for creating a button
          function CreateButton (_button, _window) {
          
               // Create function for clicking on the button
               var click = function () {
          
                    // If a window have been opened previously then hide it
                    if (lastOpened != null) {
                         lastOpened.visible = false;
                    }
          
                    // If the window isn't the same as the last one then show it
                    if (_window != lastOpened) {
                         _window.visible = true;
                         lastOpened = _window;
                    } else { // Otherwise clear last opened so that that the window can be opened again
                         lastOpened = null;
                    }
               }
          
               // Add an event listener to the button with the function stored in the click variable
               _button.addEventListener("click", click);
          }
          
          // Create buttons
          CreateButton(this.Button1, this.Window1);
          CreateButton(this.Button2, this.Window2);
          CreateButton(this.Button3, this.Window3);
          
          • 2. Re: Open and close action with single button
            ReeFiz Level 1

            Thank you. The descriptions really help!

             

            So Button1 etc. and Window1 etc. will be the Instance names of those items. I tried it but nothing appears on the stage. Here's my little test .fla file https://s3-ap-southeast-2.amazonaws.com/steve-pottery/button-test.zip

            • 3. Re: Open and close action with single button
              RandomlyFish Level 3

              The reason why you don't see anything, is because there is an error, which prevents anything from running. In chrome, you can open the console by pressing Ctrl + Shift + J on Windows, or Command + Option + J on Mac.

               

              Once the console is open, you should see an error starting with: "Cannot read property 'addEventListener' of undefined". This essentially means that the code is trying to add an event listener to something that doesn't exist.

               

              On the stage there's only Button1 and Window1, so the last two CreateButton calls are attempting to create buttons with symbols that doesn't exist. So you can just remove those two lines and it should work.

              • 4. Re: Open and close action with single button
                ReeFiz Level 1

                Of course, I thought about it too but rushed my reply. Thanks for these tips. I'll try it later when I get home.