PDA

View Full Version : Actionscript: Class callback functions.


ErikH2000
05-13-2006, 06:00 PM
Is there a not-too-hackish way to use callbacks to class methods in ActionScript? The problem is that when the method is called, "this" no longer points to the object instance. So all variables of the object instance and even other methods in the class are undefined once execution reaches the callback. Here's example code:

class CSomeClass
{
var nSomeVar:Number;

function CSomeClass()
{
nSomeVar = 1;
setTimer(OnTimer, 3000);
}

function OnTimer()
{
if (!nSomeVar) trace("This makes me sad.");
}
}
One solution I've come up with is to have the callback call another function using an instance of the class stored in a root variable. So I do this:

class CSomeClass
{
...
function OnTimer()
{ _root.m_SomeClassInstance._OnTimer(); }
function _OnTimer()
{
//Variables and methods of the instance are available now.
}
}
That works okay for singleton objects, but if I have multiple instances of a class, I won't know which one to use in the callback. Other convoluted solutions suggest themselves, but I'm looking for something that makes it all easier. Maybe there is just some feature of the language I'm missing.

Also, it's not a solution to pass an instance as a parameter, because many callbacks I need to use are predefined in ActionScript to not accept additional parameters. If I were only using my own callbacks, then sure, I could pass "this" as a param.

-Erik

UnknownGuy
05-13-2006, 07:45 PM
Yes, the changing of 'this' is a problem I've encountered aswell.

What do you mean by "predefined in actionscript to not accept additional parameters"?

I'm not sure which call back you are using, be it watch(which I use for callbacks), it allows for one additional parameter(and so does add property).

The watch command's syntax is:

Theinstance.watch(propname,functocall,parametertof unc)

So in this case, parameterfunc would be Theinstance.

In the function you are calling back, the first parameter passed back is the name, the second the old value, the third the new value, and the last your arbitrary value(so the instance).

This works fine for me.

Hope that helps!

ErikH2000
05-13-2006, 07:57 PM
Yes, the changing of 'this' is a problem I've encountered aswell.

What do you mean by "predefined in actionscript to not accept additional parameters"?
Well, like onPress() and I guess all the other event handling callbacks used for movieclips and textfields. In the example below, I don't have an option to pass extra parameters like a "this" reference to the onPress handling function.


class CSomeClass
{
function CSomeClass()
{
_root.SomeMovieClip.onPress = OnPressHandler;
}

function OnPressHandler()
{
//this isn't set to object instance.
}
}


-Erik

XIX
05-13-2006, 09:15 PM
You can do some magic with AS functions.


Sounds like you want to use Delegate?

I recommend this one over the official one.

http://dynamicflash.com/2005/02/delegate-class-refined

iopred
05-13-2006, 10:21 PM
Well, like onPress() and I guess all the other event handling callbacks used for movieclips and textfields. In the example below, I don't have an option to pass extra parameters like a "this" reference to the onPress handling function.


class CSomeClass
{
function CSomeClass()
{
_root.SomeMovieClip.onPress = OnPressHandler;
}

function OnPressHandler()
{
//this isn't set to object instance.
}
}


-Erik

As mentioned, you can use delegate, or hack it up by setting a reference to the class.


class CSomeClass
{
function CSomeClass()
{
_root.SomeMovieClip.parent = this;
_root.SomeMovieClip.onPress = function() { this.parent.OnPressHandler(this) };
}

function OnPressHandler(child:MovieClip)
{
//this function gets called, and calling 'this' inside this function refers to the class.
}
}