flash display_list_deactivation
Deactivation of graphic objects
During the development of ActionScript 3 applications, it is essential to take the deactivation of graphic objects into account. As we previously saw, when a graphic object is deleted from the display list it continues to ‘live’. In order to deactivate a deleted graphic object from the display list we use the two following events:
- Event.ADDED_TO_STAGE: broadcast when a graphic object is displayed.
- Event.REMOVED_FROM_STAGE: broadcast when a graphic object is deleted from the display.
These two extremely useful events are automatically broadcast when the player displays or is deleted from the display of a graphic object.
In the following code, the deactivation of the graphic object is not managed, when the MovieClip is deleted from the event display, Event.EVENT_FRAME is still displayed:
var myClip:MovieClip = new MovieClip(); myClip.addEventListener( Event.ENTER_FRAME, listener ); addChild ( myClip ); listener function ( pEvt:Event ):void { trace("execution"); } removeChild ( myClip );
In taking into account the deactivation of a graphic object, we listen to the Event.REMOVED_FROM_STAGE event in order to delete the listening of the Event.EVENT_FRAME event when the object is deleted from the display:
var myClip:MovieClip = new MovieClip(); myClip.addEventListener( Event.ENTER_FRAME, listener ); // listening to the eventEvent.REMOVED_FROM_STAGE myClip.addEventListener( Event.REMOVED_FROM_STAGE, deactivation ); addChild ( myClip ); listener function ( pEvt:Event ):void { trace("execution"); } deactivation function ( pEvt:Event ):void { // deletion of the listener to the eventEvent.ENTER_FRAME pEvt.target.removeEventListener ( Event.ENTER_FRAME, listener ); } stage.addEventListener ( MouseEvent.CLICK, deleteDisplay ); function deleteDisplay ( pEvt:MouseEvent ):void { // upon the deletion of the graphic object // the Event.REMOVED_FROM_STAGE event is automatically // broadcast by the object removeChild ( myClip ); }
When the graphic object is not displayed it barely uses any resources as we have taken care to delete the listener of all the resource-heavy events.
On the other hand, we can listen to the Event.ADDED_TO_STAGE event to manage the activation and deactivation of the graphic object:
var myClip:MovieClip = new MovieClip(); // listening to the eventEvent.REMOVED_FROM_STAGE myClip.addEventListener( Event.REMOVED_FROM_STAGE, deactivation ); // listening to the eventEvent.ADDED_TO_STAGE myClip.addEventListener( Event.ADDED_TO_STAGE, activation ); addChild ( myClip ); listener function ( pEvt:Event ):void { trace("execution"); } activation function ( pEvt:Event ):void { // listening to the eventEvent.ENTER_FRAME pEvt.target.addEventListener ( Event.ENTER_FRAME, listener ); } deactivation function ( pEvt:Event ):void { // deletion of the listener on the eventEvent.ENTER_FRAME pEvt.target.removeEventListener ( Event.ENTER_FRAME, listener ); } stage.addEventListener ( MouseEvent.CLICK, deleteDisplay ); deleteDisplay function ( pEvt:MouseEvent ):void { // upon the deletion of the graphic object // the Event.REMOVED_FROM_STAGE event is automatically // broadcast by the object if ( contains ( myClip ) ) removeChild ( myClip ); // on display of the graphic object // the Event.ADDED_TO_STAGE event is automatically // broadcast by the object else addChild ( myClip ); }
Within the deleteDisplay listener function we test if the timeline contains the myClip clip with the help of the containsmethod. If it is not the case we display, if it is not we delete the display.
In order to totally deactivate our MovieClip we now need to delete the references pointing to it. To do this, we modify the deleteDisplay function:
deleteDisplay function ( pEvt:MouseEvent ):void { // upon the deletion of the graphic object // the Event.REMOVED_FROM_STAGE event is automatically // broadcast by the object, the object is deactivated removeChild ( myClip ); // deletion of the reference pointing to the MovieClip myClip = null; }
You may ask what interest these two events have in realtion to a simple presonalised function which deactivates the graphic object.
Within an application, a multitude of functions or methods can delete a graphic object. Thanks to these two events we know that no matter which way an object is deleted or displayed, we know about it and we can manage the activation and its deactivation.
We will now look further into this by looking at the behavior of the player head in order to capture all of the interest of the two events that we have described.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


