Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-06-2009, 09:59 PM   #1
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 681
Default Best practice for disposing of a wrapper and its wrapee

I'm working with AS3 and I'd like to know whether the approach to disposing of a wrapper and its wrapped DisplayObject would be frowned upon in polite (or impolite) society.

So, I'm looking at doing this in the wrapper itself:

ActionScript Code:
public function dispose():void{   _wrapped.removeEventListener(Event.FOO, onFoo, false)// repeat as needed...   _wrapped.stop();   _wrapped.parent.removeChild(_wrapped);   _wrapped = null; }

Then after calling wrapper.dispose() I would assume it is safe to null the wrapper instance since I've not only dereferenced the wrapped object but removed it from its display list and nulled it out as well.

I guess my most pressing question would be 'Is it crazy to remove _wrapped from its display list from within the wrapper object?'
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Old 01-07-2009, 04:08 PM   #2
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Novosibirsk, Russia
Posts: 1,713
Default

I don't think it's a good practice to pass the responsibility from parent to child. I think parent should control its children itself... children should know nothing of parent. They only should have their methods to be called from parent instance. If you wish the parent to be informed from within a child - let the child dispatch a "remove_me" event, parent listens to it and then removes the child that has dispatched the event. This way is more flexible...
__________________
fly high

Last edited by rawmantick; 01-07-2009 at 04:10 PM..
rawmantick is offline   Reply With Quote
Old 01-07-2009, 05:13 PM   #3
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 681
Default

Thanks for the perspective romantique.

I like your suggestion, but there are two things that concern me:

1. I'm concerned that using dispatchEvent(new ClipDisposeEvent(ClipDisposeEvent.DISPOSE)); might cause some sort of race condition where you have to hope that the event is handled by the parent. Yes, you've dispatched the event, but will the event be handled before the _wrapped object is set to null 100% of the time?

2. Since the _wrapped clip should have no knowledge of its parent, how then am I to assign the event listener to its parent? The parent must be a normal movieClip instance (no special subclass applied in the library). I really don't want the wrapper class to keep track of both the wrapped clip and its parent.



edit: Here's a thought. How about this...

ActionScript Code:
public function dispose(wrappedClipParent:DisplayObjectContainer):void{   wrappedClipParent.removeChild(_wrapped); }

The object calling wrapper.dispose() does have a reference to the parent, after all. It still feels kind of odd though.
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'

Last edited by maskedMan; 01-07-2009 at 05:41 PM..
maskedMan is offline   Reply With Quote
Old 01-08-2009, 05:12 AM   #4
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Novosibirsk, Russia
Posts: 1,713
Default

Quote:
1. I'm concerned that using dispatchEvent(new ClipDisposeEvent(ClipDisposeEvent.DISPOSE)); might cause some sort of race condition where you have to hope that the event is handled by the parent. Yes, you've dispatched the event, but will the event be handled before the _wrapped object is set to null 100% of the time?
That's the point of a tree logic. Imagine a captain and a private. The private can ask the captain of something. But it's fully up to a captain if to listen to him or not. In fact you can make some "all-seeing" controller, that will have all the actor's references and will listen to their events and then execute corresponding commands. In fact why do you want to be sure that something will happen? For example when you use Loader class and is dispatches "complete" event. There is no sure someone will make something with the loader instance after it completes. It's up to you to set up a nesessary logic to make everything responsive in the way you need.

Quote:
2. Since the _wrapped clip should have no knowledge of its parent, how then am I to assign the event listener to its parent? The parent must be a normal movieClip instance (no special subclass applied in the library). I really don't want the wrapper class to keep track of both the wrapped clip and its parent.
If no special class their is - then take a look at my forst one. Probably the all-seeing controller is what you need

// update:
like the system is a set of atomic items only knowing their own local things and only having their methods to call from outside and dispatching events to outside. The point of system is to have a structure of those atomic items, listen to their events and restructure everuthing correspondingly...

In fact, you all you need is not the big "able to do everything" system, but just to remove a child from withing its own code once - forget everything, and make what you've already made Don't warm you head too much about nothing
__________________
fly high

Last edited by rawmantick; 01-08-2009 at 05:16 AM..
rawmantick is offline   Reply With Quote
Old 01-08-2009, 06:05 AM   #5
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 681
Default

Haha, indeed all it needs to do is just work. It's still very early yet in the planning phases for how this project will work and I'm just trying to find ways to handle problems that are resistant to the ever-changing whims of clients and designers.

What I do know is that no matter what solution I settle on, I'll have to do cleanup eventually and if the wrapper can't dereference and dispose of its wrapped object properly then it doesn't matter how thoroughly the wrapper itself is dereferenced by its own parent, it still sticks around forever. There are going to be enough of these guys around that I can't afford to have that.
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:22 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.