3 Replies Latest reply: Mar 14, 2012 4:44 AM by Ned Murphy RSS

    Flash Newbie; Button Code Difficulities

    Federal Blue Community Member

      I want to start an animation, but I don't want it to start until a certain button is clicked. So the first few frames will repeat (like a main menu) until the start button is clicked.

       

      I have a layer for all my script codes, and so at frame two, I have a code gotoAndPlay(1); which works great enough as it is.

       

      The problem I believe is with trying to code the button. Right now I'm just doing simple shapes and buttons that progress to the next stage that way I can figure out what I am doing wrong or how I can get it to work.

       

      I want the menu to repeat until a button is clicked, so I have the gotoAndPlay command. Then in the same frame I have another line of code underneath it, which is either not working or something is not right.

       

      Button1.addEventListener(MouseEvent.CLICK,ClickHandler);

      function ClickHandler(evt:Object):void{gotoAndPlay(3);}

       

       

      Is this the kind of code I would use for a button, if I don't want it to go to the next frame unless someone clicks on it?

       

      Thanks for any help!

        • 1. Re: Flash Newbie; Button Code Difficulities
          Ned Murphy CommunityMVP

          It's easier to read code when you follow the more standard format....

           

          Button1.addEventListener(MouseEvent.CLICK,ClickHandler);

           

          function ClickHandler(evt:MouseEvent):void{

               gotoAndPlay(3);

          }

           

          Yes, if you have a button that you place on the stage and assign an instance name of Button1 to it (via the properties panel) then the code shown above should cause the timeline it is sitting in to start playing at frame 3.... just notice that I have changed the argument type in the event handler function to indicate it is being passed a MouseEvent.

           

          Aother standard is that you only start names with capital letrters if they are class names, like MouseEvent, Object, etc.  So normally you would name your button with a lower case letter, as well as the event handler function or any variables that you add to the code.

          • 2. Re: Flash Newbie; Button Code Difficulities
            Federal Blue Community Member

            Thanks for the help so far, I've went ahead and lowercased all layers and buttons, just in case. So I have this code below in a layer labeled script, for all my coding to go into. Both pieces are in the same frame, frame 2. I also tried separating them, so that the button code went into the layer where the button was, but nothing seemed to work.

             

            gotoAndPlay(1);

             

            block1.addEventListener(MouseEvent.CLICK,ClickHandler);

             

             

            function ClickHandler(evt: MouseEvent):void{

             

            }

             

            I also made sure that the button was correctly spelled as block1, and so was the layer. Is there a step that I am missing?

            • 3. Re: Flash Newbie; Button Code Difficulities
              Ned Murphy CommunityMVP

              What you name layers has no relevance to code, so feel free to name them for your own benefit as the designer.

               

              For the code you show, you have nothing in the function, so it isn't going to do anything.

               

              If you just said you have this same code in frames 1 and 2, that is not something you want to do.  I am still not clear on why you are bouncing between frames 1 and 2, but if there is a reason for it I would put the gotoAndPlay(1) only in frame 2 and just have the rest of that code only in frame 1. 

               

              The button's instance name "block1" should be assigned in the Properties panel where you see a textfield with <Instance Name>  in it.