What are Mouse Events?
Mouse events allow us to add mouse functionality in our applications. With the use of mouse events we can make our applications respond to a mouse clicks and mouse hovers. In the example below we have the flash essential logo on the stage the logo has been converted to a movie-clip and has been given the instance name flashEssential. We'll add a MOUSE_OVER and an MOUSE_OUT event to change the alpha scale of the logo when we roll over it with our mouse cursor.
Example;
Lets go through the code;
First we need our logo to listen for the mouse over and mouse out events we do this by first giving our logo an event listener which will make it come active if you like. Inside the parameters we declare what type event we want it to listen for in this case we need a MouseEvent so we type MouseEvent allowing us to access the MouseEvent class. Then we declare what kind of MouseEvent is needed, we give them MOUSE_OVER and MOUSE_OUT respectively and finally we give both a unique name of our choice in this case we have used onHover and onOut.
flashEssential.addEventListener(MouseEvent.MOUSE_OUT, onOut);
function onHover(event:MouseEvent):void{
flashEssential.alpha = .5;
}
function onOut(event:MouseEvent):void{
flashEssential.alpha = 1;
}
We now write our functions for our onHover and onOut event listeners. We write the functions by starting out with the function keyword and then write our unique name. Inside the parameters have our arguments, we are using mouse events so we declare that we are using mouse events by writing event:MouseEvent. Inside the parentheses we set the alpha scale to .5 (50%) and 1 (100%) respectively for both onHover and onOut so that the end result is the alpha scale drops to 50% when we hover over the logo with our mouse cursor and returns to 100% when we move the cursor away from the logo.
flashEssential.addEventListener(MouseEvent.MOUSE_OUT, onOut);
function onHover(event:MouseEvent):void{
flashEssential.alpha = .5;
}
function onOut(event:MouseEvent):void{
flashEssential.alpha = 1;
}
This post is tagged AS3 Guide, Functions, mouse events
Share This Post
Jobs


























3 Comments
How can I pass arguments to a function from event handler?
You can't
Incoming Links
Leave a Reply