A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Global Variables vs Data Duplicates

  1. #1
    Junior Member
    Join Date
    Jul 2008

    Global Variables vs Data Duplicates

    I haven't done a massive amount of OO programming, but I know the basics. I've found myself using more and more AS in my Flash designs to the point where they're practically 100% AS (split quite tidily into classes etc).

    Where I fall short is a lot of the best practices... I know that global variables are to be avoided wherever possible and that passing variables to classes and functions is the way to go instead, but I've also read many times that having multiple copies of data in different places is a bad idea, which also makes perfect sense to me but these seem to conflict in certain situations.

    The problem I have with this is best illustrated with a current design issue I'm scratching my head over:

    I've got a flexible design which resizes to the window size, but I've set a maximum size (800 x 600), so there's essentially a frame within the flash design which everything is contained within - this will resize to fit the window if it's less than 800 x 600 or stay at 800 x 600 if the window's bigger than that.

    A lot of the components in my design rely on knowing the current frame size (for positioning etc), which is obviously calculated from Stage.width & Stage.height. The 3 options I can think of for handling this frame size variable are:

    1. Have a _global variable: bad practice, particularly if this movie is at some point embedded into another movie as it may conflict.

    2. Have a variable in a separate or top-level class: a bit of a referencing nightmare, every component has to be passed either a reference to this class or the variable itself (which goes against my interpretation of encapsulation) - if the latter, then problems occur as the components don't just need the variable at the moment when the window is resized (as certain animations rely on the mouse position relative to this frame) so the variable would have to be stored locally in each component = data duplication = bad practice.

    3. Calcuate the variable locally: would have to be performed every time the window is resized or whenever it's needed, not very efficient and also means data duplication = bad practice again.

    What would you do here? Is there another technique I haven't spotted? What would be considered the best programming practice?


  2. #2
    Join Date
    May 2004
    the reason people say global variables are bad is because they're not removed from memory during garbage collection - local variables are. a variable is local ONLY if it's declared using "var" inside a function. if you have some information that you need to reference within several functions, go ahead and declare it globally (and by globally, i don't mean _global - i just mean a variable declared outside a function). a few integers floating around in the timeline isn't going to affect your performance whatsoever.

    a lot of people take their entire script, stick it in a function wrapper and then call that function once - kind of like int main in C. while this will keep the debugger clean (you'll see there's nothing in the output of test movie > list variables), there's no real difference in what the VM has to manage - Flash does a pretty good job of garbage collection on it's own.

  3. #3
    Junior Member
    Join Date
    Jul 2008
    Yeah I've already got my whole script defined within a top-level class - the only script on the timeline just instances that class and the site's then built by the constructor.

    So I think I'm just going to have the "global" variables I need stored there - someone suggested implementing it as a singleton (i.e. a class with only one instance such that if you try and instance it again, it just returns a reference to the existing instance) which I think is probably quite a good idea, as then I don't have to pass references around.

    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center