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

flash actionscript : Referencing Objects using 'This'


James Fee
3/4/2004 4:59:51 PM
ok, I ran into this and this is going to sound really weird, but it works.

class test
{
var username:String = 'vince';

function loadxml()
{ var moxml:XML = new XML();
moxml.onLoad = handlereturn;
moxml.__proto__._parent = this;
moxml.load('uiconf.xml');
}

function handlereturn()
{
trace(this.toString());
var myParent:Object = this._parent;
trace(myParent.username); //this will not work because of lose of
scope
}

}



[quoted text, click to view]

0Visual
3/4/2004 9:42:32 PM
Here is my problem i have a class called test and in that class I have a
function called loadxml. So it looks something like:

class test
{
var username:String = 'vince';

function loadxml()
{ var moxml:XML = new XML();
moxml.onLoad = handlereturn;
moxml.load('uiconf.xml');
}

function handlereturn()
{
trace(this.toString());
trace(username); //this will not work because of lose of scope
}

}

Ok how can I refence back to the object from the handlereturn. The
handlereturn has no idea which instantiated object sent the load call. In
Flash MX I was able to do:
moxml.parent = this;
I would do this prior to load xml. Then the handlereturn should be able to
call this.parent and have reference back to the object. How do I do this in
2004. I tried extending the XML calls and added on member variable called
moparent. The variable moxml became an instance of this new class (let's say
sXML). However the handleresults cannot access the parent. I get a compile
error saying no property.

Anyone know of a quick and efficent way of doing this. I guess what I am
trying to do is regain scope.

Vince
0Visual
3/4/2004 10:38:51 PM
I get a compile error telling me the _proto_ cannot me found. Is your example
based on ActionScript 1.0? In Actionscript 2.0 you can't do this. That's why
I tried to extend the XML class to included this new variable.


James Fee
3/5/2004 12:08:34 PM
it is two "_" characters at both ends, not one.
It does work in AS2.0.

I have a AS2.0 class that does the following function when it is created,
one of the internal properties is a XMLsocket:
// Init
function init()
{
// initialize the object here
__XMLsocket = new XMLSocket();
__XMLsocket.onConnect = this.onConnectServer;
__XMLsocket.onClose = this.onDisconnectServer;
__XMLsocket.onData = this.onServerData;
__XMLsocket.__proto__._parent = this;
__connected = false;
if (__connectID == undefined || __connectID == null || isNaN(__connectID))
{
__connectID = -1;
}
__connectedOnce = false;
__lastPollTimer = undefined;
__reconnectTimer = undefined;
__connectRetries = 0;
__disconnect = false;
__errorMsg = "";
}

function onServerData(oData:String)
{
var myXMLconnObj:Object = this;
var myConnObj:Object = myXMLconnObj._parent;
var XMLdoc:XML = new XML(oData);
myConnObj.processData(XMLdoc);
}

this will cause my processData() function of my object to be called.

Jim

[quoted text, click to view]

glpete
3/5/2004 7:39:17 PM
I think this is a failure of AS 2.0 - it is too loose with the type checking
and then, at a same time, won't let you do something like what you want. I had
the same problem until I just declared things as Object and not their proper
type. This of course, defeats one of the benefits of type checking, but it does
allow you to do what you want.

here's a simple way:

var moxml:Object // not XML, just aObject. ths will allow you to have other
properties
moxml._parent = this;

Then later, you can access parent from handlereturn like this:

var myParent:Object = (Object)(this)._parent;

Hope this works for you.
--peter
AddThis Social Bookmark Button