dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: unreachable variable

  1. #1
    Member
    Join Date
    Jun 2002
    Posts
    61
    I've written the following function in my main timeline, at the first frame,

    Code:
    function loadConfigText() {
       loadConfigText = new loadVars();
       loadConfigText.load("config.txt");
       TESTING1="TESTING1; //just a variable to check later
    
       loadConfigText.onLoad = function(success) {
          if (success) {
             TESTING2="TESTING2"; //just a variable to check later
          }else{
             trace("not loaded");
          }
       }
    }

    I call this code from a a few levels below like this.

    Code:
    _parent._parent.loadConfigText();
    trace(_parent._parent.TESTING1);
    trace(_parent._parent.TESTING2);
    The thing is that TESTING1 is showing, but TESTING2 isn't.

    can anyone explane that to me.

    Would be great.

  2. #2
    Maya * ActionScript Addict DangerAhead's Avatar
    Join Date
    Jun 2000
    Location
    San Francisco
    Posts
    299

    missing quote mark

    You're missing a quote mark in the the TESTING1 variable.


    if you're using Flash MX try pressing CTRL+SHIFT+F in the actionscript viewer to autoformat.

    also, if there's an issue, it will refuse to format.

    you can also press CTRL+T to check for errors.

    good luck

  3. #3
    Member
    Join Date
    Jun 2002
    Posts
    61
    DangerAhead,
    Sorry for that. The Actual code is much longer.
    So I didn't copy and paste as I would normal do.
    I typed the code in here, took only the esentials
    and made that typo.
    In the actual code I didn't make that mistake.

    So the problem still stands.

    Is it maybe because it's a function within a function that
    gives me this problem?

  4. #4
    Maya * ActionScript Addict DangerAhead's Avatar
    Join Date
    Jun 2000
    Location
    San Francisco
    Posts
    299

    Email it to me.

    Mail it to me. dangerahead@yahoo.com

    I'll figure it out in a jiff.

  5. #5
    Maya * ActionScript Addict DangerAhead's Avatar
    Join Date
    Jun 2000
    Location
    San Francisco
    Posts
    299
    I tried it and it worked perfectly.
    I loaded a txt file with some variables.

    Code:
    function loadConfigText() {
    	loadConfigText = new loadVars();
    	loadConfigText.load("menu.txt");// my file -- make sure your path is good
    	TESTING1 = "TESTING1";
    	//just a variable to check later
    	loadConfigText.onLoad = function(success) {
    		if (success) {
    			this.TESTING2 = "TESTING2"; // success
    			//just a variable to check later
    		} else {
    			this.TESTING3 = "TESTING3 -- failed load";
    			trace("not loaded");
    		}
    	};
    }
    loadConfigText();

  6. #6
    Member
    Join Date
    Jun 2002
    Posts
    61
    I'm probably making it too complex!
    Anyway, I'm taking you up to your offer.
    I've send you the fla and the text files,
    maybe you can see what I'm doing wrong.

    Thanks for the time and energy you put in so far!

    Hmm, I'm just noticing you're using this.TESTING in the
    second function.

    Why is that?

    biker66

  7. #7
    Maya * ActionScript Addict DangerAhead's Avatar
    Join Date
    Jun 2000
    Location
    San Francisco
    Posts
    299
    I guess i never bothered to really grasp the difference between local and global variables in a function.

    I know, Chapter 2 of ASDG. I've read it and didn't care.

    "this" assigns it to the movie clip it's in so essentially making it accessible. it's probably not needed. It's my old troubleshooting skills coming back to haunt me.


  8. #8
    Member
    Join Date
    Jun 2002
    Posts
    61
    Jep, you're right. It isn't necessary.

    I copied your code and tried it in my project.
    I got the same result as before.

    TESTING1 is showing, but TESTING2 is unreachable.


    I thought after a night sleep I would see what the problem is,
    but nope. I don't
    So any help is still appreciated.


    [Edited by biker66 on 06-27-2002 at 02:25 PM]

  9. #9
    Member
    Join Date
    Jun 2002
    Posts
    61
    I'm curious.

    You actualy got that piece of code working like follows.

    You put that function on your main time line?
    Made a movieclip and then called the function from that movieclip with _parent path?
    like
    Code:
    _parent.loadConfigText();
    Directly after that call you placed a trace in the same movieclip as where the function is called.

    Code:
    trace("testing1="+_parent.TESTING1);
    trace("testing2="+_parent.TESTING2);
    And you got both values?

    because I'm trying and trying but it just is not working.
    I get the value for TESTING1 but not for TESTING2


  10. #10
    Senior Member
    Join Date
    Sep 2001
    Posts
    178
    Hi,
    I'm not sure if this will work, but I have an idea what your problem might be. Try this code:

    Code:
    function loadConfigText() {
       myLoadConfigText = new loadVars();
       myLoadConfigText.load("config.txt");
       TESTING1="TESTING1; //just a variable to check later
    
       loadConfigText.onLoad = function(success) {
          if (success) {
             TESTING2="TESTING2"; //just a variable to check later
          }else{
             trace("not loaded");
          }
       }
    }
    In the root timeline. I changed the name of the loadVars object because it is the same as the function, and that might cause some problems. Also, put this code in your movie clip:

    Code:
    parent.loadConfigText();
    trace("testing1="+parent.TESTING1);
    trace("testing2="+_parent.myLoadConfigText.TESTING2);
    I think that since you are defining TESTING2 in the onLoad event of the myLoadConfigText object, the scope of the variable is the object, not the root where the code is written. If I am right, then you would get to the variable by doing _parent.myLoadConfigText.variable, not just _parent.variable. Hopefully this works, Chuckles8421


  11. #11
    Senior Member
    Join Date
    Sep 2001
    Posts
    178
    I just had another thought too, since you are calling the function loadConfigText and then immediately trying to trace the variable, the onLoad event of the loadVars object might not have been called yet because the text might not be fully loaded. Chuckles8421

  12. #12
    Member
    Join Date
    Jun 2002
    Posts
    61
    Your last thought made me put some code just after de loadVars action to see when then happened and behold.

    Indeed, I was asking for the variables when the
    variables were not yet loaded.

    I fel for the oldest trick in the book.

    serves me right that I was struggling with this one for three days.

    BTW, changing the names of the objects so that it would
    be different from the function name did not only not work.
    It seems essential that they are the same.
    I don't know why, maybe someone can explain that.

    Thanks for your support y'oll.

    biker66

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