Groups | Blog | Home
all groups > flash actionscript > june 2004 >

flash actionscript : LoadVars in Classes doesnt work?


hagiflash
6/21/2004 8:04:19 PM
Hello,

i tried tu use the LoadVar in a class, something like this:
THE CLASS
class loadVarsClass {
var test_str:String;
var load_lv:LoadVars;

public function loadVarsClass(){
trace("loadVarsClass construct");
load_lv = new LoadVars();
load_lv.onLoad = showText;
load_lv.message = "initMessage";
load_lv.message2 = "initMessage2";
load_lv.message3 = "initMessage3";

test_str = "loadVarsClass test_Str";
}
public function load(){
trace("Loading");
load_lv.sendAndLoad("message.txt",load_lv,"POST");
if (load_lv == null) trace("load_lv is null");
if (load_lv == undefined) trace("load_lv is undefined");
}
public function showText(success:Boolean){
trace("onLoad is ready " + success);
if (_parent.load_lv == null) trace(" load_lv is null");
if (load_lv == undefined) trace("load_lv is undefined");

trace( load_lv.message + " " + load_lv.message2 + " " + load_lv.message3);
}
}

THE SURROUNDING (MovieClip):
stop();

myLoadVarsClass = new loadVarsClass();

Button_btn.onPress = function (){
myLoadVarsClass.load();
}
THE OUTPUT:
loadVarsClass construct
Loading
onLoad is ready true
load_lv is null
load_lv is undefined
undefined undefined undefined

THE PROBLEM:
in the Event-Handler-Function (showText) is no reference to load_lv and no
refernece to test_str.
Has anyone an idea how to work arround ?
I want to use several instnces of rhe loadVarsClass, i can't use a global
variable like _global.load_lv, becaus this will exist only once an i need
several instances of load_lv !!!.
Thanks for answering

Hagi
Laiverd.COM
6/22/2004 1:01:13 AM
As far as I can see in your code, you create a public function load, but you
never call it! And if you are; as far as I can tell, it's no use trying to
send variables to a textfile; they would obviously return undefined, as they
are not processed by a textfile.

John

--
----------------------------------------------------------------------------
-----------
RESOURCES
http://groups.google.com/advanced_group_search?hl=en&as_ugroup=*flash
----------------------------------------------------------------------------
-----------
TUTORIALS at
www.laiverd.com
Flash & PHP Emailform
Using textfiles in Flash
----------------------------------------------------------------------------
-----------

hagiflash
6/22/2004 10:34:05 AM
Hello John,

thanks for your answer!

In the Button_btn.onPress Event i call the function load(). Ok - the
load_lv.sendAndLoad command is wrong, but also the load_lv.load command doesn't
work.
The problem is the Event-Handling-Mechanismn of Flash or specific of
LoadVars???.
In my example:
+ The function 'showText' is assigned to load_lv.onLoad - Event.
+ The event onLoad occurs
+ The assigned function 'showText' is called,
B U T NOT IN THE CONTEXT OF THE CLASS-INSTANCE - WHY????
In the 'showText' function I have no possiblity to get any value from a
variable of the Class-Instance, they are all undefined or null!!

Thanks for further help

Hagi
Laiverd.COM
6/22/2004 1:22:27 PM
This works for me: but remember you cannot use sendAndLoad on a textfile and
expect to have anything else returned than what is in the textfiles. There's
now way in this setup to append to that file.

//CLASS:
class LoadVarsClass {
var test_str:String;
var load_lv:LoadVars;
var success:Boolean;
public function LoadVarsClass() {
trace("loadVarsClass construct");
load_lv = new LoadVars();
var self = this;
load_lv.onLoad = function(success) {
if (success) {
self.showText();
} else {
trace("file could not be loaded");
}
};
load_lv.message = "initMessage";
load_lv.message2 = "initMessage2";
load_lv.message3 = "initMessage3";
test_str = "loadVarsClass test_Str";
}
public function load() {
trace("Loading");
load_lv.load("message.txt");
}
public function showText() {
trace("load_lv = " + load_lv);
trace(load_lv.message + " " + load_lv.message2 + " " + load_lv.message3);
}
}
// END CLASS
John

--
----------------------------------------------------------------------------
-----------
RESOURCES
http://groups.google.com/advanced_group_search?hl=en&as_ugroup=*flash
----------------------------------------------------------------------------
-----------
TUTORIALS at
www.laiverd.com
Flash & PHP Emailform
Using textfiles in Flash
----------------------------------------------------------------------------
-----------

hagiflash
6/22/2004 5:09:40 PM
Hello,

the member 'atothek in the www.flashforum.de has found the solution:

class loadVarsClass {
var test_str:String;
var load_lv:LoadVars;
var testString:String="a simple test string for handling";

public function loadVarsClass(){
trace("loadVarsClass construct");
load_lv = new LoadVars();
load_lv.handler=this;
load_lv.onLoad = showText;
load_lv.message = "initMessage";
load_lv.message2 = "initMessage2";
load_lv.message3 = "initMessage3";
load_lv.ref_object = this;
test_str = "loadVarsClass test_Str";
}
public function load(){
trace("Loading");
load_lv.sendAndLoad("message.txt",load_lv,"POST");
if (load_lv == null) trace("load_lv is null");
if (load_lv == undefined) trace("load_lv is undefined");
}

public function showText (success : Boolean)
{
var ref_object:Object;
ref_object = this;
trace ("onLoad is ready " + success);
if (ref_object== null) trace (" load_lv is null");
if (ref_object == undefined) trace ("load_lv is undefined");
trace (ref_object.message + " " + ref_object.message2 + " " +
ref_object.message3);

trace("handler: "+ref_object.handler.testString);
};

}

this works now ver well !!!

the explanation you can find in the www.flashforum.de

The main in the explanation is:
In the (onLoad) Event-Handler function is 'this' a reference to the
LoadVars-Instance or the environment of the LoadVars-Instance and not to the
Class-Instance !!

Hagi
AddThis Social Bookmark Button