flash native_classes_stage_object

Accessing the Stage object in a secure way

As we saw in the chapter entitled Interactivity only the Stage object allows global listening to the mouse or keyboard. We should therefore modify our code in order to listen to the MouseEvent.MOUSE_MOVE event on the Stage object:

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Pencil extends MovieClip
{
public function Pencil ()
{
trace( this );
stage.addEventListener ( MouseEvent.MOUSE_MOVE, moveMouse );
}
private function moveMouse ( pEvt:MouseEvent ):void
{
trace( pEvt );
}
}
}

When testing the previous code we get the following execution error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Remember (as discussed in the chapter The display list) that the Stage property which is unique to each flash.display.DisplayObject object type, returns null when the graphic object isn’t added to the display list.

Therefore when we create the symbol:

var myPencil:Pencil = new Pencil();

The constructor is triggered and we attempt to access the Stage object. Our symbol is not yet present in the display list, the Stage property returns null and the call for the addEventListener method fails.

How do we proceed?

In the Display List chapter we discovered two important events linked to the activation and deactivation of graphic objects.

Here is a reminder of the two fundamental events:

  • Event.ADDED_TO_STAGE: this event is broadcast when the graphic object is placed in the DisplayObjectContainer present in the display list.
  • Event.REMOVED _FROM_STAGE: this event is broadcast when the graphic object is deleted from the display list.

We have to wait for the symbol to be added to the display list to be able to access the Stage object. The symbol will subscribe itself to the Event.ADDED_TO_STAGE event that will broadcast when it is added to the display list. This may seem strange but in this case the object listens to itself.

We modify the pencil class in order to integrate this mechanism:

package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Pencil extends MovieClip
{
public function Pencil ()
{
trace( this );
// the Pencil listens to the Event.ADDED_TO_STAGE event, 
Broadcasts when it is added to the display list
addEventListener ( Event.ADDED_TO_STAGE, activation );
}
private function activation ( pEvt:Event ):void
{
trace( pEvt );
}
}
}

When creating the pencil symbol the constructor is triggered:

// displays : [object Pencil]
var myPencil:Pencil = new Pencil();

When the instance is added to the display list the Event.ADDED_TO_STAGE event is broadcast:

// displays : [object Pencil]
var myPencil:Pencil = new Pencil();
// added to the display list
// displays : [Event type="addedToStage" bubbles=false cancelable=false
eventPhase=2]
addChild ( myPencil );

We define the listener method activation as private as we will not access it from outside this class. Remember to make private all of the methods and properties that will not be used from outside of the class.

The Event.ADDED_TO_STAGE event is broadcast and we can see that on continuing we listen to the target phase and that it is an event that does not participate in the bubbling phase. When the activation method is triggered we can target the Stage property* in total security and then listen to the MouseEvent.MOUSE_MOVE event:

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class Pencil extends MovieClip
{
public function Pencil ()
{
// the Pencil listens  to the Event.ADDED_TO_STAGE event
// broadcast when it is added to the display list
addEventListener ( Event.ADDED_TO_STAGE, activation );
}
private function activation ( pEvt:Event ):void
{
// listening to the MouseEvent.MOUSE_MOVE event
stage.addEventListener ( MouseEvent.MOUSE_MOVE, moveMouse );
}
private function moveMouse ( pEvt:MouseEvent ):void
{
trace( pEvt );
}
}
}

If we test the previous method we can see that the MoveMouse method is triggered when the mouse is moved anywhere in the stage.








Adobe Training center


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