7 Replies Latest reply: May 24, 2012 1:40 PM by Ned Murphy RSS

    Help with on release gotoAndPlay

    mdwebcat Community Member

      Hello -

       

      I am working on a Flash project that was using AS2.  I now need to update it to AS3.  This specific item is for a button that will Close when clicked on and then start playing at frame 400.  The button has an instance name of Close.

       

      Can anyone teach or tell me how to convert the following code to AS3?

       

      on (release) {

          gotoAndPlay(400);

      }

       

       

      Thank you!!!

        • 1. Re: Help with on release gotoAndPlay
          Ned Murphy MVP

          In AS3 you need to assign an instance name (shown as objName below)  to the object and use that to target the object via timeline code (or clas file code).

           

          objName.addEventListener(MouseEvent.CLICK, doSomething);

           

          function doSomething(evt:MouseEvent); void {

               gotoAndPlay(400);

          }

          • 2. Re: Help with on release gotoAndPlay
            mdwebcat Community Member

            Thanks Ned!

             

            I tried something like below but it is not working.  The instance name of my button is Closebtn.  I placed the code below within the Action code on the main timeline where all of my other code resides.  The actual button (Closebtn) is placed within a movie clip.

             

            Closebtn.addEventListener(MouseEvent.CLICK, onClick);

             

            function onClick(event:MouseEvent):void

            {

                 gotoAndPlay(400);

            }

            • 3. Re: Help with on release gotoAndPlay
              Ned Murphy MVP

              If the button is inside a movieclip, then assign an instance name to the movieclip and target the button thru it, for example, if you name the movieclip "mc"...

               

              mc.Closebtn.addEventListener(MouseEvent.CLICK, onClick);

               

               

              If you intend to learn AS3, one good thing to learn from the start is naming conventions.  Capitalized names are normally associated with class named (just like MovieClip, Button, TextField, etc...)   Instances of objects are normally named starting with lowercase letters.  So for what you show, normally you would use closebtn instead of Closebtn for the instance name.  If you were to assign a class name to the button you would be more likely to call it Closebtn

              • 4. Re: Help with on release gotoAndPlay
                mdwebcat Community Member

                Thank you again for helping me learn this.  I am taking an AS3 class next month.

                 

                I keep getting the following error now.  I updated the button instance name to lowercase and also gave the movie clip that the button resides in an instance name of disclosuremc.  The movie ends on frame 450 but I want the button to start at frame 400 on the main timeline (scene 1) when clicked on.  All of the other code works fine, it's just the last few lines that deal with the button named closebtn.

                 

                TypeError: Error #1009: Cannot access a property or method of a null object reference.

                    at Sample_fla::MainTimeline/frame450()

                 

                 

                Here is my AS3

                 

                stop();

                 

                Link_2.addEventListener(MouseEvent.MOUSE_UP, function(event: MouseEvent): void {

                var sURL: String;

                if ((sURL = root.loaderInfo.parameters.clickTag2)) {

                navigateToURL(new URLRequest(sURL), "_blank");

                }

                });

                 

                Link_3.addEventListener(MouseEvent.MOUSE_UP, function(event: MouseEvent): void {

                var sURL: String;

                if ((sURL = root.loaderInfo.parameters.clickTag3)) {

                navigateToURL(new URLRequest(sURL), "_blank");

                }

                });

                 

                Link_4.addEventListener(MouseEvent.MOUSE_UP, function(event: MouseEvent): void {

                var sURL: String;

                if ((sURL = root.loaderInfo.parameters.clickTag4)) {

                navigateToURL(new URLRequest(sURL), "_blank");

                }

                });

                 

                Link_5.addEventListener(MouseEvent.MOUSE_UP, function(event: MouseEvent): void {

                var sURL: String;

                if ((sURL = root.loaderInfo.parameters.clickTag5)) {

                navigateToURL(new URLRequest(sURL), "_blank");

                }

                });

                 

                legal.addEventListener(MouseEvent.CLICK, clickFunction);

                 

                function clickFunction(evt:MouseEvent):void {

                gotoAndStop(341);

                 

                }

                 

                replay.addEventListener(MouseEvent.CLICK, onClick);

                 

                function onClick(event:MouseEvent):void

                {

                     gotoAndPlay(1);

                }

                 

                disclosuremc.closebtn.addEventListener(MouseEvent.CLICK, onClick2);

                 

                function onClick2(event:MouseEvent):void

                 

                {

                     gotoAndPlay(400);

                }

                • 5. Re: Help with on release gotoAndPlay
                  Ned Murphy MVP

                  If you have new problems you should mark the current posting as answered and start a new posting.

                   

                  The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....

                   

                  - is declared but not instantiated

                  - doesn't have an instance name (or the instance name is mispelled)

                  - does not exist in the frame where that code is trying to talk to it

                  - is animated into place but is not assigned instance names in every keyframe for it

                  - is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).

                   

                  If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

                  • 6. Re: Help with on release gotoAndPlay
                    mdwebcat Community Member

                    Thank you.

                    • 7. Re: Help with on release gotoAndPlay
                      Ned Murphy MVP

                      You're welcome