6 Replies Latest reply: Jan 27, 2010 11:09 AM by Webshark2000 RSS

    Moving from AS2 to AS3, need help.

    Webshark2000 Community Member

      I learned Flash using AS2 and I'm now trying to migrate to AS3, but some of the simpliest things have changed.  I know I can't put script directly on movieclips now, but I'm not sure how to use if conditions to check for movieclips hitting certain frames, etc.

       

      Can anyone direct me to some tutorials on moving from AS2 to AS3 and some of the basic behaviors of AS3.

       

      For example:

       

         If I had a movieclip I was using as a button and I wanted it to jump to a specific frame when another movieclip on the stage hit a certain frame?  With AS2 I could simply put a script on the movieclip itself that said something like:

       

      onClipEvent(EnterFrame)

      {

           if(this._currentframe == 10)

                {

                     _parent.sampleClip.gotoAndStop(2);

                }

      }

       

      I'm not sure how to do even simple navigation code like this in AS3.

       

      Thanks for the help.

        • 1. Re: Moving from AS2 to AS3, need help.
          Ned Murphy CommunityMVP

          One thing you'll have to get used to quick is the use of the addEventListener() method.  Anything you do in AS3 that involves dealing with something happening needs to have an event listener assigned to the task... such interacting with objects, enterframe events, timing events, motion events, loading events, custom events, etc...

           

          It may be helpful if you can find a good cross reference chart to help you look up what you know in AS2 and see what it is (if it is) in AS3.  I know I found one at some point early on.  And the help documents have a tabulation of what's changed--search for "Migration".

           

          For instance, in the code you show, _currentframe becomes currentFrame, _parent becomes parent (but won't usually work unless you use MovieClip(this.parent)  ).  As for the enterframe event, that requires an event listener... so for all the code you show...

           

          this.addEventListener(Event.ENTER_FRAME, eventHandlerFunction);

           

          function eventHandlerFunction(evt:Event):void {

               if(this.currentFrame == 10)

                    {

                         MovieClip(this.parent).sampleClip.gotoAndStop(2);

                    }

          }

           

          You'll be happy to know that gotoAndPlay() still works the same (unless you are using scenes, in which case the order of the arguments has been switched).

           

          In any case, it will take you awhile to catch on to the changes, but if you stick with it , it will become as easy to use as you might find AS2.

          • 2. Re: Moving from AS2 to AS3, need help.
            dmeN Community Member

            There are lots of tutorials and some great books too. For tutorials you'll probably just want to search on whatever specific problem you're looking at. As for the example you gave, it's only a bit different in 3 and also better:

             

             

            someMovieClip.addEventListener(Event.ENTER_FRAME, checkFrame);
            function checkFrame(e:Event){
                if(someMovieClip.currentFrame == 10){
                   sampleClip.gotoAndStop(2);
                }
            }

             

            I say better because you don't need to worry about scope like you did in 2 - checkFrame isn't running in someMovieClip's context - so sampleClip is available directly, you don't need to do 'parent' to get to it.

            • 3. Re: Moving from AS2 to AS3, need help.
              dmeN Community Member

              Ned, that code won't actually work - you're using 'this' - and on the main timeline that's the MainTimeline object and it's parent is the Stage object. So MovieClip(this.parent) doesn't make sense - you'll get a type coercion failure trying to convert Stage to MovieClip. You don't need 'this' very much anymore... I can't recall the last time I even used it.


              FWIW

              • 4. Re: Moving from AS2 to AS3, need help.
                Ned Murphy CommunityMVP

                I think you're assuming you're on the main timeline.  I may be wrong, but I wasn't.  My primary goal was to demonstrate how AS3 deals with the various elements of his code.  I know that using "parent" will get you one serving of hairloss until you find out how to get it working.  And I use this more than I ever did, which wasn't much at all.

                • 5. Re: Moving from AS2 to AS3, need help.
                  dmeN Community Member

                  Yes, I was assuming mainTimeline - if I ever put code in Flash it would only be in the mainTimeline - I wouldn't ever put code inside a clip... So, ok I see your point.

                  • 6. Re: Moving from AS2 to AS3, need help.
                    Webshark2000 Community Member

                    Thanks for the help. The addEventListener with ENTER_FRAME helps me out a ton.  I can use it to create timers and variables to keep track of what frame a movieclip is on, etc.  That's a really big help for me.  I tried the following code to hold a movieclip's current frame number in a variable which I can then use to tell other movieclips what to do depending on the number.  Let me know if any of my syntax is incorrect.

                     

                     

                    var MCFrameNum;


                    sampleMovieclip.addEventListener(Event.ENTER_FRAME, sampleFunction);


                    function sampleFunction(e:Event)

                    {

                         MCFrameNum = sampleMovieclip.currentFrame;

                    }

                     

                     

                    Thanks Again!