8 Replies Latest reply on Nov 2, 2009 9:49 PM by Anilone

    Document class & Frame based code

    Anilone

      When using "Document class" in .fla file,  Can we remove frame specific ActionScript codes to Document class?

       

      For example say i have 3 lines of AS code in Frame 1 and 10 lines of code in frame 2.

      When the the frame 1 is executing, the corresponding code from Document class should get execute.

      How I create Document class to handle this?

       

      If it is possible please give an example for the same.

       

      Thanks

      Anil

        • 1. Re: Document class & Frame based code
          Kalisto Level 4

          In document class you may listen for enter frame event and that process what you want.

          Something like that:

          package

          {

          import flash.display.MovieClip;

          import flash.events.event

          public class MainDoc extends MovieClip

          {

          public function MainDoc ()

          {

          addEventListener( Event.ENTER_FRAME, onenterFrame, false, 0, true )
          }

           

          private function onenterFrame( evnt: Event ): void

          {

          switch( currentFrame )

          {

          case( 1 ): //CODE FOR FRAME 1

               break;

          case( 3 ): //CODE for frame 3

               break;

          }

          }

          }

          }

          • 2. Re: Document class & Frame based code
            Anilone Level 1

            Thanks Kalisto.

            Your suggestion is good, but I am doubt that it would help to solve the issue.

            My ultimate focus is to make flash application fully Object Oriented and remove code from frames and put it in class files.

             

            But the Event.ENTER_FRAME listener invokes continuously (fps) and our frame code execute as many time as the frame per second given.

             

            What I am looking for is something similar to the switch you given in the onenterFrame handler and get a currentFrame number as switchExpression

            So that the code code execute only once for a particular frame.

             

            Hope I could clarify the requirement completely.

             

             

            Thanks once again for your time.

             

            Regards

            Anil

            • 3. Re: Document class & Frame based code
              Kalisto Level 4

              You may create a  classes and associate it with the movieclips setted in the timeline( each clip will have a own class ). If you do this the constructor of the class  will be executed every time when the playhead enters in the frame. In this case there will be no need to have a swtich in the main doc as.

              If you using a switch and listener for the ENTER_FRAME in the mainDoc as you can see all the code and all the logic ( For small piece of code this is a good approach )

              • 4. Re: Document class & Frame based code
                Anilone Level 1

                >>If you using a switch and listener for the ENTER_FRAME in the mainDoc as you can see all the code and all the logic ( For small piece of code this is a good approach)

                 

                I tried it, but the problem is it is getting executed continuously!

                 

                switch( currentFrame )

                {

                case( 1 ):

                          trace("Execute code of Frame: 1 " )

                     break;

                 

                case( 2 ):

                     break;

                }

                 

                Output of this is

                Execute code of Frame: 1

                Execute code of Frame: 1

                Execute code of Frame: 1

                Execute code of Frame: 1

                Execute code of Frame: 1

                .......................

                ....................

                 

                We need to execute the code only one time right? How can we achieve it?

                 

                Thanks and Regards

                Anil

                • 5. Re: Document class & Frame based code
                  Kalisto Level 4

                  It is easy. After executing of all code that you want remove event listener

                  removeEventListener( Event.ENTER_FRAME, "FUNCTION NAME: I 'M NOT REMEBER IT'S NAME' )

                  • 6. Re: Document class & Frame based code
                    Anilone Level 1

                    I have tested the following. It worked. The flow is like

                     

                    1. On entering a frame (Say Frame 1), removeEventListener ENTER_FRAME

                    2. Execute the code of Frame 1

                    3. Call play to continue

                    4. addEventListener to attach eneter frame listeners for next frames if any

                    5. My doubt is this a recommended method? Do you anticipate any issues on this approach? I believe Adobe should provide something better to remove code from the time line for managing big projects OR any alternative is there/hidden we are not aware of? ( I am C++ guy with less thna 3 months AS3/Flash experience only! )

                     

                         switch( currentFrame )
                                {           
                                case( 1 ):               
                                    removeEventListener(Event.ENTER_FRAME ,onEnterFrame);
                                    trace("Execute Frame 1 code and then go to next frame");
                                    play();
                                    addEventListener(Event.ENTER_FRAME ,onEnterFrame , false, 0, true );
                                 break;
                                    
                                case( 2 ):
                                    removeEventListener(Event.ENTER_FRAME ,onEnterFrame);
                                    trace("Execute Frame 2 code and then go to next frame if any");
                                 break;
                                }

                     

                    Thanks

                    Anil

                    • 7. Re: Document class & Frame based code
                      Kalisto Level 4

                      Belive or not but the timeline programming is not recommended for big project. So use whenever it is posible classes.

                      about switch lets look closer this:

                        case( 1 ):               
                                      removeEventListener(Event.ENTER_FRAME ,onEnterFrame); <-remove a listener
                                      trace("Execute Frame 1 code and then go to next frame");
                                      play(); <- this is used to tell the playhaed if it is stopped, but I belive that this is not the case
                                      addEventListener(Event.ENTER_FRAME ,onEnterFrame , false, 0, true ); <- add same listener. There is no need to do this.
                                   break;
                      this is equal to:
                      case( 1 ):               
                                     trace("Execute Frame 1 code and then go to next frame");
                                      play();
                                   break;

                       

                      What kind of code you want to execute? Give me a sample.

                      • 8. Re: Document class & Frame based code
                        Anilone Level 1

                        I am slowly making a MMORPG for social networking sites facebook/MySpace etc.

                        As a C++ programmer, i wish to do use Object oriented design for the same.

                         

                        First frame contains AS3 code for user authentication.

                        The code do the following things....

                         

                        1. Load and parse settings file and read all settings

                        2. Get information about the social networking site where it is hosted.

                        3. Say we are on Facebook, then it use FaceBook API's for authentication.

                        4. On authentication success, gotoAndPlay(2) next frame

                         

                        Frame 2 code do the following

                        1. Create objects of Server class to connect to server

                        2. Create other classess required

                        3. Then invoke initialization

                        4. Remaining codes are on corresponding class files and

                        5. I have included a "functions.as" in the timetime, which contains code that supposed to be in the timeline.

                         

                        I wish to remove complete code from the .fla file ( and timeline) by giving a document class. But i couldn't make it so far.

                         

                        Please advise

                         

                        Best regards

                        Anil