Click to See Complete Forum and Search --> : How do I reference a class method from listener.onLoadInit()?


cdk57
11-11-2006, 11:05 AM
For example:

class Photo() {
public function Photo() {
var loader:MovieClipLoader = new MovieClipLoader();
var listener:Object = new Object();
_root.objRef = this;
listener.onLoadInit = function() {
_root.objRef.doSomething();
}
loader.addListener(listener);
loader.loadClip(url, containerMC);
}

public function doSomething():Void {
trace("loaded");
}
}


As you can see, in order to call the method doSomething() once the clip has been loaded, I'm having to store a reference to the object in _root. What's the proper way of doing this?

For example, is there a way to pass a reference to the object into the onLoadInit() anonymous function through a parameter?

Thanks

schizo
11-13-2006, 11:49 AM
I tried my own little test, and it seemed to work just fine. For one, you sould change your class name to just Photo and not Photo(). Also, I wasn't sure how you instantiated the Photo class, but it should have been something like:


var myPhoto:Photo = new Photo();


See the attached zip.

cdk57
11-13-2006, 12:24 PM
Thanks schizo,

My mistake on the naming of the class as Photo() - was copying some lines from a more complex class.

I'm invoking the class in the way you suggested, and the class has been working as expected for me using the _root.objRef method.

Although it's working, I was wondering if there's another way of doing it without having to create a reduntant variable on the _root every time I want to access an object with an anonymous function?

schizo
11-13-2006, 01:13 PM
Unfortunately you will most likely always need an object reference of some sort. I usually store the reference in the object in question however, rather than on the root level. For example:

class Test
{
public function Test()
{
var mc_test:MovieClip = _root.attachMovie("mc", "mc", 1);
mc_test.obj_ref = this;

mc_test.onRelease = function()
{
trace(this.obj_ref.doSomething());
}
}

public function doSomething()
{
return "woot!";
}
}


Optionally, in this case you could create your own MovieClip class which inherits the Macromedia/Adobe one. You could then provide your custom class with methods to get and set the object reference.

Hope this helps!