onRemove not called for timeline objects in Flash Player 9

Nick's Avatar

Nick

06 May, 2011 12:19 PM

Hi there,

I've encountered a problem where all my Mediator onRemove handlers are not being triggered.

I am developing using Flash CS5, targeting Flash Player 9. (FP9 is a requirement for the audience of the current project I'm working on).

As an example (source attached) I have a document class that contains a 2-frame MovieClip. The second frame of the MovieClip contains a context-mediated object. I start the movie with the 2-frame MovieClip stopped at frame 1. Clicking the stage toggles the 2-frame movie clip between its frames. When the second frame appears, the mediated sprite appears and traces a 'registered' message to output. Clicking again to jump back to the first frame, hence removing the mediated object from the stage, onRemove fails to be triggered - you can see this from the lack of trace.

Switching the Flash to target FP 10 causes the the onRemove to be successfully triggered.

Also, manually adding and removing objects from the stage (using addChild and removeChild) causes normal onRemove behaviour.

Many thanks,

Nick

  1. Support Staff 1 Posted by Stray on 06 May, 2011 12:43 PM

    Stray's Avatar

    Hi Nick,

    this is a bug in early versions of FP 9. The stage doesn't send the Event.REMOVED_FROM_STAGE event unfortunately. (In very early versions the REMOVED_FROM_STAGE event type doesn't even exist).

    You've got a couple of options, one of which is to manually dispatch the Event.REMOVED_FROM_STAGE event on behalf of the object.

    You can do this either by relaying (listening for Event.REMOVED, cloning the event, changing the type to REMOVED_FROM_STAGE and then redispatching) at a high level, or by dispatching it manually within your views themselves.

    There's a discussion here that covers some of the issues (you won't have all of them):

    http://knowledge.robotlegs.org/discussions/problems/74-fp-9016

    Feel free to come back with more specific questions... which approach you need to take will depend a lot on your code base (Eg whether these views all share a base type that you can edit).

    Stray

  2. 2 Posted by Nick on 06 May, 2011 01:17 PM

    Nick's Avatar

    Hi Stray,

    Thanks so much for your prompt reply.

    I'm not sure that's the cause of the problem. The onRemove is failing to be triggered when just testing using the test player straight from Flash CS5.

    I've put in handlers with traces for ADDED_TO_STAGE and REMOVED_FROM_STAGE which show that these events are being fired for the mediated object.

    Attached is the updated source with these handlers.

    I actually initially detected the problem because onRemoves weren't being triggered when running my project in a browser with Flash Player 10.

    Bit of a mystery...

    Many thanks,

    Nick

  3. Support Staff 3 Posted by Stray on 06 May, 2011 01:44 PM

    Stray's Avatar

    Hi Nick - ok, so for the purposes of narrowing down...

    Can I just confirm that what you're saying is that your contextView is firing the Event.REMOVED_FROM_STAGE event, but that for some reason the mediatorMap isn't responding to this event?

    I thought you said "Switching the Flash to target FP 10 causes the the onRemove to be successfully triggered." - but you're now saying that FP 10 is experiencing the problem as well?

    A simple but possibly not viable for you solution - could you use addChild/removeChild in the frame code to add and remove the instances?

    Stray

  4. 4 Posted by Nick on 06 May, 2011 02:15 PM

    Nick's Avatar

    Hi Stray,

    Sorry, I wasn't clear.

    Publishing to target Flash Player 9, but running in Flash Player 9 or 10, causes onRemoves not to be fired.

    If you modify the Flash to target Flash Player 10, onRemove seems then to be triggered successfully.

    Also my example test file wasn't correctly testing the problem. I now monitor ADDs and REMOVEs in the context view (attached). You'll see that both handlers get fired when adding and removing the mediated sprite, but that the sprite mediator's onRemove fails to trigger whenever the Flash is published to target player 9.

    I presume RL is listening for ADDs and REMOVEs on the context view as opposed to ADDED_TO_STAGEs and REMOVED_FROM_STAGEs as only the former get bubbled. Have I got that right - might be that I'm not understanding how RL is doing its monitoring of display object additions and removals.

    Many thanks for your help,

    Nick

  5. Support Staff 5 Posted by Stray on 06 May, 2011 02:27 PM

    Stray's Avatar

    Hi Nick, no problem.

    The mediatorMap listens for ADDED_TO_STAGE and REMOVED_FROM_STAGE on the contextView.

    As you rightly identify - this event doesn't bubble. Robotlegs doesn't rely on Event bubbling at all. The ADDED_TO_STAGE / REMOVED_FROM_STAGE events are dispatched by an object when something is added TO it, or removed FROM it (including the whole nested stack).

    You can look at mediatorMap internals here:

    https://github.com/Stray/robotlegs-framework/blob/master/src/org/ro...

    My understanding is that FP 9 is flaky about when / how it fires those REMOVED_FROM_STAGE events. It is most flaky in the early version, where these events don't even exist, but it's flaky in all FP 9 versions.

    There are 2 places where it could be flaking out in your scenario:

    1) Using the timeline to add / remove instances might not be triggering the events on the container views (contextView is the only one we care about!)

    2) As you move back and forth through the timeline you get some interesting stuff going on in terms of which instances are the 'same' instance (===) and which are a new instance (==) of the same class.

    So - no point monitoring ADD / REMOVE - it's ADDED_TO_STAGE and REMOVED_FROM_STAGE that we're interested in, and only as fired by the contextView.

    Hopefully that helps with a bit more digging...

    Stray

  6. 6 Posted by Nick on 06 May, 2011 02:58 PM

    Nick's Avatar

    Hi Stray,

    Thanks for this info.

    Okay, I think it's most likely due to what you say with the ropiness in which FP9 is handling ADDED_TO_STAGE and REMOVED_FROM_STAGE.

    In this case, what I think I'll do (since my clean up code can happily be executed multiple times without ill-effect), is manually force a call to onRemove. I found a quite elegant single line using Signals that can be simply placed into the onRegister handler of mediators:

            new NativeSignal(this.view, Event.REMOVED_FROM_STAGE, Event).
                addOnce(function(e:Event):void { onRemove(); });
    

    since REMOVED_FROM_STAGE seems to be fired correctly in FP9 and FP10 when used immediately on the view like this.

    Thank you for all your help.

    Nick

  7. Support Staff 7 Posted by Stray on 09 May, 2011 01:34 PM

    Stray's Avatar

    Nice solution!

    No problem - I'll close this now, but feel free to reopen it if you hit any problems.

    Stray

  8. Stray closed this discussion on 09 May, 2011 01:34 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac