RSS link icon

Learn mouse events with actionscript 3 

 

Hi again all, this time we will take a look at how to "catch" simple mouse events, this might be useful for simple flash menus or games, I made this small example when I was doing a simple flash game and needed the function.

Basically what this flash actionscript tutorial will do is to let you see how to catch the mouse is moving, movie down and mouse up events, and just telling you in a text field, as you can test below.

We will start by making a dynamic text field and give it a name, so with the text tool drag out a text field on the stage, then go the properties panel and change the text type to dynamic (so it can receive info) and give it an instance name. (I named mine "mouse_events").

mouse events tutorial

Now we will go to the actionscript panel and type in some code.

A have made the code description in between the code with comment tags // so you can just delete them if you want.

//These three eventlisteners are telling flash to "listen" for specific events
//in our case if the mouse moves and if the mouse button is down or up.
//then if one of these events happens, it calls a function (mouse_down, mouse_up or moving).
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse_down); 
stage.addEventListener(MouseEvent.MOUSE_UP, mouse_up); 
stage.addEventListener(MouseEvent.MOUSE_MOVE, moving);

// This is how to construct a simple function,
// this function will write in our text field "mouse is moving"
// when the even is raised by the eventlistener
function moving(e:MouseEvent) 
{ 
	mouse_events.text = "mouse is moving"
} 
// function just like above, just for the mouse down event. 
function mouse_down(e:MouseEvent) 
{ 
	mouse_events.text = "Mouse button is down"
} 
// function just like above, just for the mouse up event.
function mouse_up(e:MouseEvent) 
{ 
	mouse_events.text = "Mouse button is up again"
} 

ronald.a.b says: Tuesday, April 22, 2008

ok, very good, thanks from Lima-PerĂº.


DamnEdge says: Sunday, April 06, 2008

Sweet, that will help me out. Is there a way to get rid of the text when the mouse is not on the stage


Md. Aslam Aslam says: Monday, March 31, 2008

Very Nice Fro Teaching Experience online
Thanks You


Admin Bob says: Saturday, March 08, 2008

Hi, well not in the comment section, but I might make a tutorial about it later, so check back in the flash tutorials section later and you might find one


virendra says: Thursday, March 06, 2008

hello,
Thats cool, can u tell plz me , how to zoom images and scroll with scripts
regards


raviya says: Tuesday, February 12, 2008

hellooo

   


 

 

 

 7

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact