jeudi 25 novembre 2010

Using mouse input

The mouse input, using the eventListener


The mouse is more versatile than a key!

Handling the mouse also creates events like the keys.
We can attach reactions like the ones we practiced with the keys also to the mouse.
Again we use an eventlistener:

stage.addEventListener (.... )
also we have to add a function which defines what to do with the events triggered:
stage.addEventListener (...., whatToDoWithTriggerFunction)
the whatToDoWithTriggerFunction will have to be defined.

Here are several eventlisteners for the mouse (different kind of triggers, clicking moving):
  • addEventListener(MouseEvent.CLICK, clickHandler);
  • addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
  • addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  • addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
  • addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
  • addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
Then in the function we can track different kind of properties:
 For example the mouse point has x and y coordinates.

So the recepie is like this:

add an eventListener:
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

and the corresponding reaction:

 function mouseUpHandler( event:MouseEvent): void
{
var posX : Number = event.stageX;
var posY : Number = event.stageY;
myMovieClip1.x = posX ;
myMovieClip1.y = posY ;
}

Aucun commentaire:

Enregistrer un commentaire