A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [F8] Variables

  1. #1
    Disenchanted
    Join Date
    Jan 2007
    Posts
    25

    [F8] Variables

    Hi it's me again !

    I've made a timer which is a dynamic text box and will start counting when the game starts. I've also made a stop button for the user to stop the game and go to the next scene which will display the time used.

    But how should I 'save' the time used in the game scene and use it in the 'displaying-the-time-used' scene?


    Hope you can understand what I said =P
    Flash newbie

    Long Live The Black Parade

  2. #2
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282
    Set the variable as _global and it will be available to your entire movie

  3. #3
    Disenchanted
    Join Date
    Jan 2007
    Posts
    25
    Quote Originally Posted by estudioworks
    Set the variable as _global and it will be available to your entire movie

    Thanks for answering!!!


    but how?? can you explain a bit more?
    Flash newbie

    Long Live The Black Parade

  4. #4
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282
    Global variables are useful when you want a variable to be accessible from anywhere in your movie, even in different scenes. It's also a good thing to declare a variable as global rather than put it in the _root, because putting everything there can cause some problems. Of course, you have to be sure that there is no other variable called like that somewhere else in the movie.

    The syntax is quite simple - all you have to do is add _global in front of the variable.

    Code:
    _global.myVar=5;
    Simple. But as always, you have to be careful. If you want to change, or access this variable, you have to put _global in front too.

    Code:
    trace (_global.myVar); 
    // returns 5

    Actually, if you don't put _global, it will also work. But let's look at this example:

    Code:
    _global.myVar=5; 
      
    for (i=0;i<5;i++) 
    { 
      myVar++; 
    } 
      
    trace (myVar); 
    // returns 10 
    trace (_global.myVar); 
    // returns 5
    Mmm? What happened? Why is the result different?

    Let's look at what we did. First we initialize a global variable to 5, and then we increment in a for loop the variable without the keyword _global in front. And when we trace the variables, with and without _global, they have different values.

    Not putting _global made Flash create a variable in the variable space of the timeline, giving it the default value of the global variable, and masking the global variable in this timeline. That's why we don't manipulate the global variable, but the local variable.

  5. #5
    Disenchanted
    Join Date
    Jan 2007
    Posts
    25
    ahhh I see! I'll try it now.

    Thanks!! =D
    Flash newbie

    Long Live The Black Parade

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