2011-09-12

FLASH CS3 - Action Script part 4 Mouse Event (basic)

As we learn from previous script in Flash MX, we will notice that flash not only work like movie (Start from beggining and end after the movie end or rewind from start). Flash able to capture mouse and keyboard Event.

To learn both is same like previous part (enterFrame). Unlike before, this script not make the aplication receive the event directly. We will to use something to capture the event. For mouse, there are a few mouse type event.

  • MouseDown. if the button is clicked (left I think?)
  • MouseUP. if the button is release (Left I think)
and there is another for right. But in this tutorial we will create for left. 



package {
    
import flash.display.Sprite;
    
import flash.events.MouseEvent;

    public class 
ExampleApplication extends Sprite {
        private var 
_sprite:Sprite;

        public function 
ExampleApplication(  ) {
            
_sprite = new Sprite(  );
            
addChild(_sprite);
            
_sprite.graphics.beginFill(0xffffff);
            
_sprite.graphics.drawRect(10, 20300300);
            
_sprite.graphics.endFill(  );


            
_sprite.addEventListener(MouseEvent.MOUSE_DOWNonMouseDown);
            
_sprite.addEventListener(MouseEvent.MOUSE_UPonMouseUp);
        }
//main function

        
private function onMouseDown(event:MouseEvent):void {
            
_sprite.graphics.lineStyle(101);
            
_sprite.graphics.moveTo(mouseXmouseY);
            
_sprite.addEventListener(MouseEvent.MOUSE_MOVEonMouseMove);
        }

        private function 
onMouseUp(event:MouseEvent):void
        
{
            
_sprite.removeEventListener(MouseEvent.MOUSE_MOVEonMouseMove);
        }

         private function 
onMouseMove(event:MouseEvent):void {
            
_sprite.graphics.lineTo(mouseXmouseY);
        }

    } 
//public class
    
}


like previous explanation:


import flash.display.Sprite;
import flash.events.MouseEvent;
we need Sprite and mouseEvent to continue. Soo we import everything needed in this script
public class ExampleApplication extends Sprite {
public function ExampleApplication( ) {
i called this as main function. remember this name has to be same as filename (minus the .as)
_sprite = new Sprite( );
make variable sprite type as Sprite
addChild(_sprite);
_sprite.graphics.beginFill(0xffffff);
_sprite.graphics.drawRect(x1, y1, x2, y2);
_sprite.graphics.endFill( );
create new box from point X1,Y1 to X2,Y2
_sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
this is the script that listen to mouse event! for advance use, the whole movie is the place where mouse Event will respond. And don't forget where function will run
private function onMouseDown(event:MouseEvent):void {
_sprite.graphics.lineStyle(1, 0, 1);
_sprite.graphics.moveTo(mouseX, mouseY);
_sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
Create a line and move the first target to where your Mouse position (X,Y) then when the mouse move.. it will follow
private function onMouseMove(event:MouseEvent):void {
_sprite.graphics.lineTo(mouseX, mouseY);
}
The event will follow the cursor.. but remember.. this must be done while you hold the mouse
private function onMouseUp(event:MouseEvent):void
{
_sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
the function will cancel above function to make sure the above not run (create line).

Next I will try to explain how to capture keyboard

Tidak ada komentar: