A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: loadVars.onload doens't work in function?

  1. #1
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    hi all,

    I got no problem with loadVars in the main stage but when I placed it in a function inside a movie clip (exactly a smart movie clip) like this:

    Code:
    function Init() {
    	this.createTextField("tf", 1, 0, 20, 300, 0);
    	this.tf.multiline = true;
    	this.tf.autoSize = "left";
    	this.tf.selectable = false;
    	this.tf.wordWrap = true;
    	this.tf.border = false;
    	this.tf.type = "dynamic";
    	myTextFormat = new TextFormat();
    	myTextFormat.font = "Verdana";
    	myTextFormat.size = 10;
    	myTextFormat.color = 0xffffff;
    	myTextFormat.leading = 1;
    	this.tf.setNewTextFormat(myTextFormat);
    
    	// may have trouble here ...............
    	loadVarsText = new loadVars();
    	loadVarsText.load(fileName);
    	loadVarsText.onLoad = function(success) {
    		if (success) {
    			this.tf.Text = this.varName;
    		}
    	};
    	// end of may have trouble here .................
    }
    When I debuged, the lines of code in the loadVarsText.onload function didn't work

    Any idea? Any suggestion?

    Thanks in advance



  2. #2
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749

    Which this is this?

    Just a case of these not this.

    This in the first function refers to whatever, whereas this in the onLoad refers to the LoadVarsText object.

  3. #3
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    First, thanks for answering

    Yeah, I known the bug fell in that line of code
    Code:
    this.tf.text = this.varName;
    but I don't know how to correct.

    The "this" in the first few lines refer to the movie (smart movie) itself and it worked (when I debuged).

    I've also tried with
    Code:
    this._parent.Text = this.varName;
    and
    Code:
    this.tf.Text = loadVarsText.varName;
    but none of them worked !

    Waiting for any idea, any suggestion !

  4. #4
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Tried this without the function bit (but that shouldn't change anything) inside a movie clip and it worked for me...

    this.createTextField("tf", 1, 0, 20, 300, 0);
    this.tf.multiline = true;
    this.tf.autoSize = "left";
    this.tf.selectable = false;
    this.tf.wordWrap = true;
    this.tf.border = true;
    this.tf.type = "dynamic";
    myTextFormat = new TextFormat();
    myTextFormat.font = "Verdana";
    myTextFormat.size = 10;
    myTextFormat.color = 0xff0000;
    myTextFormat.leading = 1;
    this.tf.setNewTextFormat(myTextFormat);

    // may have trouble here ...............
    loadVarsText = new loadVars();
    loadVarsText.load("test1.txt");
    loadVarsText.onLoad = function(success) {
    if (success) {
    tf.text = this.varName;
    }
    };
    // end of may have trouble here .................

    Changed the 3 bold lines...

    1-Obviously if your background is white, you wouldn't see the text.
    2-Put in an actual file name, just for my testing.
    3-Eliminated the "this" before the textfield instance name, and use the correct syntax which is a small "t" on "tf.text", but that syntax error doesn't change the fact that the text from the text file does show up!

  5. #5
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    1-Obviously if your background is white, you wouldn't see the text.
    2-Put in an actual file name, just for my testing.
    3-Eliminated the "this" before the textfield instance name, and use the correct syntax which is a small "t" on "tf.text", but that syntax error doesn't change the fact that the text from the text file does show up!
    1- My background is black but it's a good idea to make it red (work in both black and white background). Thanks !
    2- Sorry, I didn't tell you that fileName and varName are the 2 defined parameters ( I intended to make a smart clip)
    3- Has changed

    OK, I think I had better tell you what I intend to do.
    I intend to make a smart clip (which 2 parameters I mentioned above) to load different text when its parameter is changed on-the-fly.
    So, I need a function inside of it and when I debuged, the function actually was called.

    But, the onload function of loadVarsText wans't called at all??

    I don't know how to post my fla. I will send you by mail if you don't mind.

    Thanks



  6. #6
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    Code:
    function Init () {
    	this.createTextField ("tf", 1, 0, 20, 300, 0);
    	this.tf.multiline = true;
    	this.tf.autoSize = "left";
    	this.tf.selectable = false;
    	this.tf.wordWrap = true;
    	this.tf.border = false;
    	this.tf.type = "dynamic";
    	myTextFormat = new TextFormat ();
    	myTextFormat.font = "Verdana";
    	myTextFormat.size = 10;
    	myTextFormat.color = 0xffffff;
    	myTextFormat.leading = 1;
    	this.tf.setNewTextFormat (myTextFormat);
    	// may have trouble here ...............
    	loadVarsText = new loadVars ();
    	loadVarsText.load ("someText.txt");
    	loadVarsText.onLoad = function (success) {
    		if (success) {
    			_root.tf.text = this.varName;
    		}
    	};
    	// end of may have trouble here .................
    }
    I'm not sure what the problem is but I hard coded the actual location of where the tf textfield is created (_root.tf.text) and I was able to successfully load in my text file ("someText.txt") and have the value displayed on screen using this code.

    Of course if you aren't building the textfield on the _root then you would have to manually change the _root.tf.text to the "absolute" location of the textfield you just created.

    The thing that sucks is that intuitively I would want to say

    Code:
    loadVarsText.onLoad = function (success) {
    		if (success) {
    			_parent.tf.text = this.varName;
    		}
    	};
    and it doesn't work. If you list variables in test movie you will see:

    _level0.loadVarsText = blah blah blah
    _level0.tf blah blah blah..

    _parent just does not get the scope outside of the loadVarsText abject ( I know, weird isn't it ). This is not the only time I have experienced this madness.

    Only when I said, "Fine, I'll &$%#$^! make it a @$%#^$* absolute path." (um I left out all the swearing... sort of ), did it &#^$&@! work.

    Im done blabbing now..

    Good ^#&$%#$ Luck to you!!

    time to have a cigarette

  7. #7
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    Of course, I can't use _root because it's a smart clip and will be reused.

    So ????


  8. #8
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    Well I missed the part about the smartclip so I put it inside of a movieclip and then dropped _root or any other path altogether and it worked out. Although it did not work when I tried to run the Init() function on the same frame that I instantiated the movieClip.

    Code:
    loadVarsText = new loadVars ();
    	loadVarsText.load (filename);
    	loadVarsText.onLoad = function (success) {
    		if (success) {
    			tf.text = this.varName;
    		}
    	};
    Maybe that will work out for you.

  9. #9
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    I was looking for and debugging the wrong bug!

    Sorry to waste your time. But it's a new problem now.

    The line of code
    Code:
       tf.text = this.var1;
    worked but it worked only with var1, which is same as what I typed in the text file. It didn't, however, when I changed it into varName. So the problem is that when I used the name of the defined parameter (it's a smartclip) it didn't work at all.

    I defined that variable of type String.

    Any idea?
    Thanks in advance. Waiting.



  10. #10
    Junior Member
    Join Date
    Jul 2001
    Posts
    10

    same problem here

    loadVarsText.load ("someText.txt");
    loadVarsText.onLoad = function (success) {
    if (success) {
    _root.tf.text = this.varName;
    }


    should be

    loadVarsText.onLoad = function (success) {
    if (success) {
    _root.tf.text = this.varName;
    }
    loadVarsText.load ("someText.txt");

    Your defining an event.... to be run after the vars are loaded..... least thats what I have read.... Down side is I haven't been able to get it to work with a loadMovie. weird thing is... it still runs my function.. Hope I helped ....

  11. #11
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    huynt-

    Would you mind either posting a link to or emailing a .fla to me? At this point I need to see this smartclip in action because I doubt what I have been doing to help you out looks anything like your authoring environment.

    cul8r

  12. #12
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    hi lunapero, thanks for answering but as I've said, it worked when I change varName to var1 so the problem was not the order (I supposed)

    ====

    I had decided to put in a trace command to be sure that the smart clip really received the parameter I sent to it. And indeed it did. I meant it output var1 to the output window. The strange point is that the line of code

    Code:
    this.varName
    still didn't work while
    Code:
    trace (varName)
    worked perfectly ???

    ========
    hi Antibody,

    As I've said, I don't know how to post my fla. I certainly will email you but what's your email address?

    The link email lead to a window that doens't have a file attachment option ??

    ======

  13. #13
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    Woops.. I thought I added it to my last post..

    kevan@kevan11.com

    Here you go.


  14. #14
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    Hi huynt-

    I got your .fla, thanks for sending it.

    I have been assuming that you wanted to define varName from a textfile, is this correct?

    The reason I ask is that I see on the Init button you have:

    mcSmartText.varName="var1";

    So I got confused as to how you want to define varName, through a text file or by naming varName on the Init button.

    Too be honest, with the solution that I am presenting it doesn't matter, varName just needs to be defined one way or the other. Could you try this code out and tell me if it does what you want?

    Code:
    function Init () {
    	var sFileName = fileName;
    	this.createTextField ("tf", 1, 0, 20, 300, 0);
    	this.tf.multiline = true;
    	this.tf.autoSize = "left";
    	this.tf.selectable = false;
    	this.tf.wordWrap = true;
    	this.tf.border = false;
    	this.tf.type = "dynamic";
    	myTextFormat = new TextFormat ();
    	myTextFormat.font = "Verdana";
    	myTextFormat.size = 10;
    	myTextFormat.color = 0xff0000;
    	myTextFormat.leading = 1;
    	this.tf.setNewTextFormat (myTextFormat);
    	loadVarsText = new loadVars ();
    	loadVarsText.load ("test.txt");
    	trace (varName);//this will display the Init button's definition of the var, in this case 'var1'
    	loadVarsText.onLoad = function (success) {
    		if (success) {
    			with (this) {//sets scope to 'mcSmartText'
    				var sVarName = varName;
    				tf.text = sVarName;//changed this.sVarName to sVarName
    			}
    		}
    	};
    }
    [/b]*as you can see the only lines I changed were in the .onLoad function.[/b]


    What I notice is that if I leave in the variable definition on the Init button, as is (mcSmartText.varName = "var1"; ) , then 'varName' as defined in the text file is what shows up in tf.text, presumably because loading in the variable 'varName' happens after the Init button's definition.

    The second thing I tried was to change the variable name in the text file, from varName = Antibody to varNameX=Antibody, and what do you know, the variable definition on the Init button is what showed up. The great thing is that either way I did it, *something showed up in the tf.text field* besides 'defaultValue', which I believe is what you want.

    Let me know if this works out for you.

  15. #15
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    Hi Antibody,

    Sorry for having you wait for a while. I was so busy the last few days.
    First of all, your solution worked ! (greatly thanks )
    It's not necessary to declare sVarName variable.
    About the way defining the var, what I did had in the text file named "test.txt" was:

    &var1=something....

    Here's your changed code
    Code:
    if (success) {
        with (this) {//sets scope to 'mcSmartText'
          tf.text = varName;
         }
       }
    Oh, my GOD, as I wrote this reply to you, I played with it one more time and very strangely, this also worked
    Code:
    if (success) {
          tf.text = this.varName;
       }
    while it didn't work previously. I really don't know why? I am getting confused.

    Unfortunately, I didn't keep a copy of the original one so that I can know what wrong I have done.
    If you still keep it, please send it back to me.

    I have sent the new one together with the text file to you.

    Regards,

  16. #16
    HELP>>>ACTIONSCRIPT DICTIONARY
    Join Date
    Feb 2000
    Location
    In the Present Moment
    Posts
    1,041
    if (success) {
    tf.text = this.varName;
    }

    Im surprised that this worked too. I tried it before as well and no go, but hey it works now so shhhh don't tell Flash and I won't.

    BTW I sent the fla back, it should be in your inbox.

    Until we meet again.


  17. #17
    Senior Member
    Join Date
    Jan 2001
    Posts
    106

    on the way another problem

    hi guys,

    There is another problem in OnLoad event of FlashMX....there is lag of few milliseconds for the event to get fired. This is there for onLoad of sound as well as data....have you noticed

    eyes are open
    gnana

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