Skip navigation
abobo37
Currently Being Moderated

pass variables between classes

Jul 17, 2012 12:56 PM

Tags: #cs3 #script #class #3.0 #action #code #variable #game #level #pass

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

 
Replies
  • Currently Being Moderated
    Jul 17, 2012 2:00 PM   in reply to abobo37

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 18, 2012 8:03 PM   in reply to abobo37

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 19, 2012 6:56 PM   in reply to abobo37

    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!

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 20, 2012 10:50 AM   in reply to abobo37

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points