Mouse Events Actionscript 3 in Flash cs3 Tutorial

This tutorial will teach you about the very commonly used MouseEvents class used in actionscript 3 within Flash CS3. You will learn how to set up event listeners and handlers for all of the Mouse events withing the MouseEvents class. Mouse events can be used in any application, game, user interface or interactive animation you create and can give the user instant feedback on where to click and what to do next. This tutorial requires you have Basic understanding of programming with either Actionscript 3 or any other language and have a intermidiate knowledge of how to use the Flash user interface.

Your Ad Here

Open a new .fla file in Flash CS3 and save it as MouseEventsTute.fla then open a new .as file and name it the same MouseEventsTute.as

Then in the Flash Porperties pannel where the text input field named Document Class is, type MouseEventsTute. This is so the class runs in the flash player at run time and is assigned as the main class to this .fla file.

Now for the code

//I started by writing the package that our class will be written in
package {
//then add the required import statements for the classes that i need during runtime
    import flash.display.Sprite;

    import flash.events.MouseEvent;
   
    //then i set the public class with the same name as the file name this is
    // very important and all class names must be the same as the .as file name
    public class MouseEventsTute extends Sprite {
   
    // the constructor function must also have the same name as the class and
    //file names.
    //in this public function i called another private function that is written
    //further on.
        public function MouseEventsTute() {

            allEvents();

        }
       
    //here i set he private function as it is handling all the events thats what i
    // decided to call it.
        private function allEvents():void {
   
    //i drew out a small rectangle on to the stage this is so the mouse has something
    // to be tested on.
            var sprite:Sprite = new Sprite();

            addChild(sprite);

            sprite.graphics.beginFill(0xCCCCCC);

            sprite.graphics.drawRect(200, 200, 50, 80);

            sprite.graphics.endFill();
           
           
    //I wrote out all the event listeners for all the possible mouse events and attached
    // them to the sprite every time so that each of them can be tested as the occur over
    //it.
                sprite.addEventListener(MouseEvent.CLICK, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.MOUSE_OVER, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.ROLL_OUT, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.ROLL_OVER, onMouseEvent);
                sprite.addEventListener(MouseEvent.DOUBLE_CLICK, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseEvent);

               

                sprite.addEventListener(MouseEvent.MOUSE_OUT, onMouseEvent);

        }
   
    // then i set another public function to handle what happens when the flash player
    // picks any of these events. In this case it will trace each event and display the
    // event type in the out put window
        public function onMouseEvent(event:MouseEvent):void {

            trace(event.type);

        }

    }

}

The output window should display the following

 

If you enjoyed this post, make sure you subscribe to my RSS feed

!

Comments

4 Responses to “Mouse Events Actionscript 3 in Flash cs3 Tutorial”

  1. mitesh on August 30th, 2007 12:42 pm

    Can not find viraltag on your blog.so posting comment here.
    I have added your link at

    http://humor-in-photos-and-pictures.blogspot.com/2007/08/viraltag-link-exchange-program.html

    Pls add my link as ‘Humor pictures’

    http://humor-in-photos-and-pictures.blogspot.com/

    6:38 AM
    Delete

  2. PhamViet on December 28th, 2007 4:16 am

    Ths for your tutorial, it very usefull for me :)

  3. Mike on February 11th, 2008 9:40 pm

    Hello,

    I am contacting you on behalf of www.flashcomponents.net . I’ve read one of your tutorials and I like the way you write. Our site is continuously growing and we recently added a tutorial section. We kindly ask for your approval to allow us to publish your tutorials on our site, mentioning you as the author.
    Of course, we are inviting you to do it yourself, but either way, it would be our pleasure to publish them.

    Kind regards,
    Mike | Flash Components Team

  4. dz on February 15th, 2008 4:27 am

    aadadasdasda

Leave a Reply




  • Sites We Like


  • UK Life Insurance Quotes
    Life Quotes
    Illness Insurance
    Contents Insurance
    Web Site
    Your Ad Here
    Secured Loans

    Compare secured loans to find the perfect deal for you!

    www.accepted.co.uk

    Matched.co.uk