8 Replies Latest reply: Jul 20, 2012 2:53 PM by abobo37 RSS

    pass variables between classes

    abobo37 Community Member

      Hi! I'm new to flash actionscript and i'm making a small game right now.I was thinking about creating seperate movieclips for each game level and assign seperate classes to them so it's more clean and easy to code.

       

      eg:-

       

      level1-level1.as

      level2-level2.as

      level3-level3.as

       

      My problem is how do pass variables(like health,score,etc)to the next level?

      plz help i'm stuck on this and i searched a lot but couldnt get the right answer..

       

      oh,and i'm using flash cs3 with actionscript 3.0

        • 1. Re: pass variables between classes
          esdebon Community Member

          in your class you need define public vars

           

          public var variable:Number;

           

          Youn need a main Class to import  all the another class in this

           

          then you can call the variables

           

          ClassName.variable

          • 2. Re: pass variables between classes
            abobo37 Community Member

            i'm sorry i understood the public var part and how to call the variables  but how do i import the other classes?

            can you give an example code plz?

            • 3. Re: pass variables between classes
              Kyle517

              I don't think you truly understand what movieclips and classes are.

               

              MovieClips are display objects; your character, the enemy, items, platforms < these will be movieclips

               

              Classes can be used to define new elements or extend existing ones such as an Object or a MovieClip.

               

              For the purposes of making a game, I would separate the levels by frames.

               

              make level 1 on a frame labeled "level 1"

              and level 2 on a frame labeled "level 2"

               

              variables will carry from frame to frame.

              use gotoAndStop(frame_label) to switch between levels

               

              *make sure to use stop(); on the first frame to stop from cycling between frames.

              *you can make separate frames between each level that has the code "gotoAndStop(previous level)" to prevent people from hitting "play" on the swf and cycling through your levels.

               

              you can also look up SharedObjects as a method of storing variables even after the swf is closed.

              • 4. Re: pass variables between classes
                abobo37 Community Member

                thanks for the answer it was REALLY helpful... i decided to use shared objects since i only need to pass about 5 variables each level.

                and by the way,what i meant by "movieclip for each level" was take enemies,player and stuff like that  and put them inside another mc which is the level...it may not be a good method,but i'm just a beginner soooo

                 

                p.s. and also,the game i'm making doesnt fit into one genre,it's more like different stuff in each level(level1-shooting,level2-platformer...)so its really confusing to write it all in one place.

                • 5. Re: pass variables between classes
                  Kyle517 Community Member

                  No Problem sounds like Warioware.

                   

                  Ah, so you want to have a holder MovieClip that stores all of the other MovieClips for each level.  Do you plan on removing the MoveClip via removeChild() and adding the new level via addChild()?

                   

                  Just some words in advance:

                   

                  If removing the holder MovieClip gives you errors, you may have to clear the holder MovieClip of it's children and it's event listeners before removing the holder itself.

                   

                  while (holder.numChildren > 0)

                  {

                       holder.removeChildAt(holder.numChildren-1);

                  }

                   

                  ^ fast and effective way of clearing the holder of it's children.

                   

                  and also:

                   

                  The method of separating levels by frame is also a good way to organize your actionscript.  The actionscript for each level would go in the same frame as the level, so it would be separated.  The only drawback is the "cycling" I talked about above, and you would have to remove any event listeners before moving to the next frame.

                   

                  That is all, Good Luck!

                  • 6. Re: pass variables between classes
                    abobo37 Community Member

                    haha i was wondering how to remove mcs thanks  4 the code example

                    oh by the way,do u mean there's a way to add seperate codes for each frame to run when it loads?(did u mean the actions panel in the timeline?,coz i thought it was unprofessional to put important codes in the actions panel )

                     

                    sorry if i'm bothering you with all those questions

                    • 7. Re: pass variables between classes
                      Kyle517 Community Member

                      Click on a frame > open actions-frame panel

                       

                      Haha, you have it backwards .  It's unprofessional to put code in separate MovieClips, because it's messy to have parts of the same code on many different MovieClips!

                       

                      Are you using as3? because as3 won't even allow you to place code on a MovieClip anymore, unless it's on a frame inside of the MovieClip. (as2 does allow it)

                       

                      And yes, you can have different code on each frame that works only for that frame; variables and event listeners from the previous frame are the only things that are passed over from frame to frame.

                       

                      And it's fine, I enjoy helping

                      • 8. Re: pass variables between classes
                        abobo37 Community Member

                        oh i see..i didnt know that..

                        yeah i'm using as3 but it worked when i tried it!i think thats because i didnt put the code in the actions panel in the mc.i made a .as file with the same name of the mc so when the mc loads,the .as file will also load.i guess its not a good idea to do that..

                         

                        btw,i thought i should postpone making the game coz its toooo boring to do it alone..i mean i have 2 code it,draw the graphics,add music and do all that stuff before the summer vacation ends...so im gonna just read some manga & play pokemon

                         

                        THNX ANYWAY FOR ALL UR SUPPORT (maybe i'll do it in the christmas break )