seventeen
seventeen

Reputation: 443

Actionscript 3: Passing classes as parameters

Is it good practice to pass a sub class as a parameter to another function, because I wish to perform modification to a property of the sub class.

Specifically I wish to set the class to

alpha = 0.5;

I would normally of made the sub class a private property of the main class and access it that way. However, I wondered if this is a better or worse way?

Like this:

        var d:DElem = new DElem(text);          
        addChild(d);
        d.addEventListener(MouseEvent.CLICK, Proxy.add(this, click, d));


        private function click(event:MouseEvent, elem:DElem):void {
            Animate.fadeOutIn(elem);
        }

Upvotes: 1

Views: 214

Answers (2)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Looking at your code, I would definitely recommend against it. Unless the code you've shared only ever runs once for the entire application, you will most likely have a memory leak.

When you add an event listener to an object, that event listener will keep the object in memory even if it's otherwise ready for garbage collection (not used anymore). When you add an event listener with an anonymous function (which is presumably what is returned from Proxy.add, you then have no easy way to remove the event listener. Doing d.removeEventListener(MouseEvent.CLICK, Proxy.add(this, click, d)) will not work, because Proxy.add returns a new/different function everytime it's used, so even though code-wise it's the same as when you added the listener, since the object/function returned is different it will not actually remove the listener.

You could pass true to the weak flag (5th parameter) when you add your listener:

d.addEventListener(MouseEvent.CLICK, Proxy.add(this, click, d), false, 0, true);

Which tells FlashPlayer to ignore the listener when figuring out which objects to free from memory. While this would then reduce your memory leak potential, it's still not a very clean way. Though it is good practice to either always use the weak flag, or always explicitly remove your listeners when no longer needed.

As alluded to in the comments and other answer, you actually don't need to use your Proxy class, the following will accomplish what you'd like:

d.addEventListener(MouseEvent.CLICK, click, false, 0, true);

private function click(event:MouseEvent):void {
    Animate.fadeOutIn(event.currentTarget as Sprite);
    //event.currentTarget is a reference to the Object that you attached the listener to
    //event.target is the object that actually dispatched the event, which could be the same as event.currentTarget, or a child of it
}

Upvotes: 2

FilippoG
FilippoG

Reputation: 586

Yes it is good practice, according to your application architecture. There are many situations in which you can send a Class as a method parameter. But looking at your code, you are not sending a Class as a parameter, but an instance of DElem Class, which is also a good practice. Some events carry with them the target of (i.e. mouse events), that you can access with event.target or event.currentTarget. Although, to grab compile-time errors you might find it more convenient to also send the target correctly typed as a method parameter.

Upvotes: 1

Related Questions