7 Replies Latest reply on Jul 29, 2011 10:41 AM by Rothrock

    how to access a buttons state with code

    jack77766 Level 1

      Hi,

       

      I already made some buttons that I was planning on using as a menu for difficulty, easy,medium, hard. The buttons are already made with all the states and graphics, so I wanted to add an event listener so  that when you click a button for example easy, if the medium button was at its hit state I would revert it back to up, eg:

           easyButton.addEventListenere(MouseEvent.CLICK, function(e:event) { mediumButton.gotoAndStop("up") or mediumButton.up

       

      but then I realized there is no propertie or function in the simpleButton class to change the state of the button,  I know I can make a movieclip, and do gotoAndStop at the up frame, but is there a way to make it work as a button???

        • 1. Re: how to access a buttons state with code
          Rothrock Level 5

          Nope. That is the point of not being able to control the state of a button with code: there isn't a code that lets you control its state.

           

          What it seems you are after is what I would call a toggle button--like a light switch it looks different when it has been "flipped."

           

          You will need to either use a movieclip and add code to change its appearance or two different simple buttons and toggle the visibility of the two different buttons. I reccomend the movieclip approach.

          1 person found this helpful
          • 2. Re: how to access a buttons state with code
            jack77766 Level 1

            ah.. .but why dont they want us to access the button with code? would make things easier.

             

            Thanks though Im gonna use the adding another thing and playing with visibility since I already have the buttons made.

            • 3. Re: how to access a buttons state with code
              Rothrock Level 5

              Buttons in Flash go back a long long ways. In early versions you couldn't even access buttons by name and they were really twitch and odd. Most serious Flash users dropped them and developed buttons based on movieclips.

               

              I'm not really sure why they didn't fix them when they updated the to AS3, but there you go! I'm guessing it has something to do with the idea of keeping them simple.

               

              As for the visibility, be sure that you are setting the visible property and not the alpha. A button with zero alpha will sill be rolloverable, clickable, etc.

              • 4. Re: how to access a buttons state with code
                jack77766 Level 1

                Thanks for the answer, good to know.

                 

                Yeah the buttons are done, used the visible property in the code, TY!

                • 5. Re: how to access a buttons state with code
                  Ned Murphy Adobe Community Professional & MVP

                  You can control the states of SimpleButton objects with AS3.  If you look at the properties you will see that the button has four states that you can assign... upState, overState, downState, and hitTestState.  Each of these states are DisplayObjects.  You can reassign a state to assume another DisplayObject using code.  So if you wished to have a button retain the downState when it is clicked and moved away from, you can assign its upState to be its downState...

                   

                  btn.upState = btn.downState;

                   

                  and it will happen.  But if you want to restore the upState to what it originally was, then you need to store that somewhere so that it can be re-assigned.  If it's only the one state, the hitTestState can come in handy for that.  But when you start wanting to have a variety of controls, it can get a little juggly.

                  1 person found this helpful
                  • 6. Re: how to access a buttons state with code
                    jack77766 Level 1

                    Hi Ned,

                     

                     

                    I did see those properties in the docs but since it said it takes a displayObject I didnt really pay attention to them.

                     

                    Nice way of making them work though, I didnt think of passing one state to another like that, I might have used that method and just have a temp button to store the original up state of the button thats pressed.

                     

                    Thanks for the tip

                    • 7. Re: how to access a buttons state with code
                      Rothrock Level 5

                      Thanks for pointing that out Ned. I didn't know you could do that.

                       

                      It is similar to how some of the Components worked in AS2. I haven't tried to use it with SimpleButton, but I did muck with it a bit in AS2 and it did get pretty complex and confusing if I was trying to do very much.

                       

                      I'll have to give it a whirl and see if it is any better than I remember.