all groups > flash actionscript > october 2005 >
You're in the

flash actionscript

group:

scope of vars in Actionscript 2


scope of vars in Actionscript 2 stephan.k
10/15/2005 9:45:49 PM
flash actionscript: Hello Forum.

I'm again in a similar situation: A function can't see another function
because of scope reasons. How do I solve this one? I can't figure out how to
make the loadFlvPath() function see the traceMe() function... the problem is
that recXML is an XML Object through which I can't pass through a "this"....
Any ideas from the OOP MAsters out there?

Many thanks

S.


/////////////////////////////////////

class com.website.VideoManager {

//Various vars... [...]

public function VideoManager(){
connectToDB();
}

private function connectToDB(){

//various xml and loadvar commands [...]

sendLV.sendAndLoad(con,recXML);
recXML.onLoad = loadFlvPath;
}

private function loadFlvPath(success){
if(success){
traceMe(); //doesn't see the function traceMe >> How do I get this to work?
}
}
private function traceMe(){
trace("me");
}
}
Re: scope of vars in Actionscript 2 Fruber Malcome
10/15/2005 10:11:10 PM
Normally - this is what I'd do - why couldn't you pass recXML this way?
Otherise - is it possible to make the trace function _root or as a member of a
base class that all of your classes are derived from? or even a static member
of a global singleton class?

class com.website.VideoManager
{

//Various vars... [...]

public function VideoManager():Void
{
this._connectToDB();
}

private function _connectToDB():Void
{
//various xml and loadvar commands [...]
recXML.onLoad = function( bSuccess:Boolean ) { _loadFlvPath( recXML,
bSuccess ); }
sendLV.sendAndLoad( con, recXML );
}

private function _loadFlvPath( bSuccess:XML ):Void
{
if( bSuccess )
{
this._traceMe(); //doesn't see the function traceMe >> How do I get this to
work?
}
}

private function _traceMe():Void
{
trace("me");
}
}// end of class

Hope this helps, I don't have too much information to go on.

thanks - Fruber.

Re: scope of vars in Actionscript 2 stephan.k
10/15/2005 10:29:57 PM
Hi Fruber

Thanks for your reply.

I tried your code but it has the same problem. Basically once I'm in the
recXML.onLoad part there is no reference to the class itself anymore. So I
can't call any of the functions of the VideoManager class.

Unfortunately I don't know enought about OOP so the static concept is a
little foreign to me. Maybe I should go back to the books before proceeding.
Also I'm not sure what you're refering to as the base class. If there's a
simple way of putting it I'd appreciate it if you can guide me into a
direction...

sk.

//////// >> your suggestion was this: ///////////

recXML.onLoad = function( bSuccess:Boolean ) {
_loadFlvPath( recXML, bSuccess ); ///>> recXML is undefined when I
trace it. and _loadFlvPath can again not be triggered cause it's in a different
scope.
}


Re: scope of vars in Actionscript 2 LuigiL
10/16/2005 12:00:00 AM
Now, instead of a local reference as in your previous post, you use a static
reference:

class com.website.VideoManager {

// class property
private static var thisObj:com.website.VideoManager;

public function VideoManager(xmlPath:String){
thisObj=this; // setting the static reference
connectToDB(xmlPath);
}

private function connectToDB(xmlPath:String){
// do something here
myXML.onLoad = loadFlvPath;
}

private function loadFlvPath(success:Boolean){
if(success){
// do something here
}
thisObj.traceMe(); // Now it does see the function
}

private function traceMe(){
trace("Called");
}
}

Inside the loadFlvPath method, when parsing the XML you need to use the static
reference too:
thisObj.myXML.firstChild.chilNodes.length // example
Re: scope of vars in Actionscript 2 stephan.k
10/16/2005 12:00:00 AM
Very interesting! thank you very much. If you a little bit of time would you
mind writing a little comment about "static". Advantages / Disadvantages /
Guidlines / Things to look out for...

Thanks again.

Best

stephan


Re: scope of vars in Actionscript 2 2m
10/16/2005 12:00:00 AM
I'm kindda sorry to jump in again, but I Would like to over an other solution,
without contradicting LuigiL:

class com.website.VideoManager {

//Various vars... [...]

public function VideoManager(){
connectToDB();
}

private function connectToDB(){

//various xml and loadvar commands [...]


// Here you set loadFlvPath to be executed onLoad of sendLV.
// That means (like with your movieclip example before) that your function
// will be executed in the scope of the Object that calls the event.
// That woul be sendLV. But sendLV does not know (yet) of the object that
// created it and is member of your class. So when working with build-in
ClassObject
// that define onEvent handlers like loadVars or Moviecliip... It is alwasy a
// good idea to assign a reference to the creating Object to the
event-handling one.
// Lets call it myMaster.

sendLV.myMaster = this;
sendLV.sendAndLoad(con,recXML);
recXML.onLoad = loadFlvPath;

}

private function loadFlvPath(success){
if(success){

// so this is sendLV and myMaster is the (back-)reference to the object, that
is a member of your class.
// so traceMe should execute on that Object.

this.myMaster.traceMe();
}
}
private function traceMe(){
trace("me");
}
}

I'm suggesting that, because the problem with the static reference could be,
that the static property would be overridden by the next object that is created
by that class.
Re: scope of vars in Actionscript 2 stephan.k
10/16/2005 12:00:00 AM
Hi

Thanks again. I got your solution to work finally...

I ended up doing it this way: (same idea as you've suggested)

[...]
sendLV.sendAndLoad(con,recXML);
var thisObj:VideoPath=this; //reference to current class
recXML.onLoad = function( bSuccess:Boolean ) {
thisObj.loadFlvPath( this,bSuccess );
}
[...]

the recXML.onLoad wasn't able to see the thisObj if I set it as a propery of
the sendLV.

So a static properties of a class is reset everytime a class is instantiated?
and the previous objects derived from that class will take over the most
current value of the static property? Do I understand that correctly?

Thanks to everyone. This has been extrememly helpful.

Stephan


AddThis Social Bookmark Button