dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Variables losing their data :(

  1. #1
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    15

    Variables losing their data :(

    Hi guys! Seems like I'm forever posting here at the moment!

    I'm having a problem where AS seems to be losing data that I've stored in variables when it loops back to the start of a movie clip. I'm not sure why, because I'm not re-declaring the variables everytime at the start, they get defined in a button function.

    Basically, what I'm trying to do is set a flag variable, and another called 'continueOnFrame'.

    When the movie loops back around, it checks to see if the flag has been set to 1. If it has, then it goes to the frame number contained in 'continueOnFrame'.

    Or at least, that's what it SHOULD do. I ran a trace on the variables, and the button function sets them fine, but when the movie lops it sets them to 'undefined' again

    Here is the code for frame 1:
    Code:
    stop();
    
    trace("Flag = "+flag+" / continueOnFrame = "+continueOnFrame);
    
    if (flag == 1) {
      	gotoAndPlay(continueOnFrame);  
    };    
    
    news_btn.onRelease = function() {
      	trace("pressed");
      	gotoAndPlay(2);  
    };
    
    about_btn.onRelease = function() {
      	trace("pressed");
      	gotoAndPlay(61);
    };
    And here is the code which actually defines the variables (frame 35 of the movie):
    Code:
    stop();
    news_btn.onRelease = function() {
    	trace("news_btn pressed");
    	var flag = 0;
    	var continueOnFrame = 0;
    	trace("Flag set to: "+flag+" / continueOnFrame set to: "+continueOnFrame);
    	gotoAndPlay(36);
    };
    about_btn.onRelease = function() {
    	trace("about_btn pressed");
    	var flag = 1;
    	var continueOnFrame = 61;
    	trace("Flag set to: "+flag+" / continueOnFrame set to: "+continueOnFrame);
    	gotoAndPlay(36);
    };
    contact_btn.onRelease = function() {
    	var flag = 1;
    	var continueOnFrame = gotoAndPlay(36);
    	trace("success!");
    };
    I'll attach the .fla file as well if it helps. I'm using MX2004 (although, I've saved the file in MX format).
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    remove the "var" before "flag".

  3. #3
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    15
    Cheers for your response ericlin.

    How will that help?

    How will AS know that flag is a var? And why remove it from flag but not from continueOnFrame?

    I'm not saying you're wrong, I'd just like to know the logic.

  4. #4
    Senior Member
    Join Date
    May 2001
    Posts
    1,838
    When you declare a variable as "var flag", that flag is a "local" variable or what we say "temporary" variable. It only exists in this function. It is different from the "flag" outside of this function.

    You have "var flag" in news_btn.onRelease function, about_btn.onRelease function and contact_btn.onRelease function. Those "flag"s are all local to that function. They will not affect one another.

    If you remove the "var", then the "flag" is a variable outside of the function.

  5. #5
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    15
    Brilliant! I just tried it and it works!

    Thanks so much 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