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, 20, 300, 300);
_sprite.graphics.endFill( );
_sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}//main function
private function onMouseDown(event:MouseEvent):void {
_sprite.graphics.lineStyle(1, 0, 1);
_sprite.graphics.moveTo(mouseX, mouseY);
_sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseUp(event:MouseEvent):void
{
_sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseMove(event:MouseEvent):void {
_sprite.graphics.lineTo(mouseX, mouseY);
}
} //public class
}
like previous explanation:
import flash.display.Sprite;we need Sprite and mouseEvent to continue. Soo we import everything needed in this script
import flash.events.MouseEvent;
public class ExampleApplication extends Sprite {i called this as main function. remember this name has to be same as filename (minus the .as)
public function ExampleApplication( ) {
_sprite = new Sprite( );make variable sprite type as Sprite
addChild(_sprite);create new box from point X1,Y1 to X2,Y2
_sprite.graphics.beginFill(0xffffff);
_sprite.graphics.drawRect(x1, y1, x2, y2);
_sprite.graphics.endFill( );
_sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);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
_sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
private function onMouseDown(event:MouseEvent):void {Create a line and move the first target to where your Mouse position (X,Y) then when the mouse move.. it will follow
_sprite.graphics.lineStyle(1, 0, 1);
_sprite.graphics.moveTo(mouseX, mouseY);
_sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseMove(event:MouseEvent):void {The event will follow the cursor.. but remember.. this must be done while you hold the mouse
_sprite.graphics.lineTo(mouseX, mouseY);
}
private function onMouseUp(event:MouseEvent):voidthe function will cancel above function to make sure the above not run (create line).
{
_sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
Next I will try to explain how to capture keyboard
Tidak ada komentar:
Posting Komentar