3 Replies Latest reply on Dec 1, 2009 11:00 PM by kglad

    #include question

    shintashi

      What is the proper way to include actionscript code when the code you want to include has both variables and onEnterFrame actions?

       

      for example, if I had two scripts;

       

      //script A, to be included

      var car_spd = 25;

      onEnterFrame(){

      if(Key.isDown(Key.RIGHT)){car._x += spd;}

      }

       

       

       

      //script B, the file include is added to

       

      var truck_spd = 22;

      onEnterFrame(){

      if(Key.isDown(Key.RIGHT)){truck._x += spd;}

      }

       


      -----------

       

      would I use #include "car.as"  on the line after var truck_spd = 22;

       

      or in the onEnterFrame?

       

      or would I make two car files, like

      car_variables.as

      car_actions.as

       

      and then include actions inside the onEnterFrame, and the variables outside the onEnterFrame?

        • 1. Re: #include question
          kglad Adobe Community Professional & MVP

          if you want to add both scripts to one use:

           

          #include "scriptA.as"

          #include "scriptB.as"

           

          or, if you wanted to add scriptB to scriptA, you could use:

           

          #include "scriptB.as"

           

          in scriptA.as

           

          but no matter what you do, you'll have a problem.  you can't have two onEnterFrame methods applied to the same movieclip.  well, you can but only the first one will be overwritten by  the 2nd.  you need to apply those loops to different movieclips or incorporate the code from both into one onEnterFrame method.

          1 person found this helpful
          • 2. Re: #include question
            shintashi Level 1

            so to play it safe do you think  should stick the onEnterFrame stuff in functions mostly?

             

            I know this works some of the time but there's a weird "instaneous" problem with things like acceleration. For example:

             

            function gravity(mc){
            acc = 0;
            acc++;
            mc._y += acc;}

            onEnterFrame = function(){
                  gravity(ball_mc);
            }


             

            is treated as velocity, while

             

             

            acc = 0;

            function gravity(mc){
            mc._y += acc;}

            onEnterFrame = function(){
               acc++;   gravity(ball_mc);

             

            is treated as acceleration; I'm still confused as to why, probably explaining my nervousness of using include.

            • 3. Re: #include question
              kglad Adobe Community Professional & MVP

              using #include is exactly the same as if the code in the included file is pasted in place of the #include line of code.  if you don't 100% understand that, re-read that sentence.

               

              in fact, you should not use #include unless you completely understand what that's doing.  the benefits of using it are minimal for you.  and the drawback is that you don't completely understand it.

               

              it's better that all the code in that file (or those files) be attached to that timeline so there's a better chance you understand what you're doing.

              1 person found this helpful