Actionscript 3.0 编程实例三 鼠标draw and drag (写给初学的朋友)
代码可直接运行, 拖动有一点缺陷。 鼠标超出播放器释放后 会回不到原点。。。欢迎优化。
/**
* liveTea
*/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.geom.Point;
import flash.display.DisplayObject;
public class DrawDragTest extends Sprite
{
private var _drawing:Boolean;
private var _circle:Sprite;
private var beginLocation
oint;
public function DrawDragTest()
{
graphics.lineStyle(1,0,1);
_drawing=false;
stage.addEventListener(MouseEvent.MOUSE_DOWN,drawBegin);
stage.addEventListener(MouseEvent.MOUSE_MOVE,drawing);
stage.addEventListener(MouseEvent.MOUSE_UP,drawEnd);
//
_circle=new Sprite();
_circle.graphics.beginFill(939878393,1)
_circle.graphics.drawCircle(20,20,30);
_circle.graphics.endFill();
addChild(_circle);
_circle.addEventListener(MouseEvent.MOUSE_DOWN,dragBegin);
_circle.addEventListener(MouseEvent.MOUSE_UP,dragEnd);
}
private function dragBegin(event:MouseEvent):void{
beginLocation=new Point();
beginLocation.x=event.target.x;
beginLocation.y=event.target.y;
//event.target.
//event.target.
event.target.startDrag();
event.target.filters=[new DropShadowFilter()];
}
private function dragEnd(event:MouseEvent):void{
event.target.stopDrag();
event.target.filters=null;
event.target.x=beginLocation.x;
event.target.y=beginLocation.y;
}
private function drawBegin(event:MouseEvent):void{
graphics.moveTo(mouseX,mouseY);
_drawing=true;
}
private function drawing(event:MouseEvent):void{
if(_drawing)
{
graphics.lineTo(mouseX,mouseY);
}
}
private function drawEnd(event:MouseEvent):void{
_drawing=false;
}
}
}
/**
* liveTea
*/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.geom.Point;
import flash.display.DisplayObject;
public class DrawDragTest extends Sprite
{
private var _drawing:Boolean;
private var _circle:Sprite;
private var beginLocation
![](http://bbs.actionscript3.cn/images/smilies/default/tongue.gif)
public function DrawDragTest()
{
graphics.lineStyle(1,0,1);
_drawing=false;
stage.addEventListener(MouseEvent.MOUSE_DOWN,drawBegin);
stage.addEventListener(MouseEvent.MOUSE_MOVE,drawing);
stage.addEventListener(MouseEvent.MOUSE_UP,drawEnd);
//
_circle=new Sprite();
_circle.graphics.beginFill(939878393,1)
_circle.graphics.drawCircle(20,20,30);
_circle.graphics.endFill();
addChild(_circle);
_circle.addEventListener(MouseEvent.MOUSE_DOWN,dragBegin);
_circle.addEventListener(MouseEvent.MOUSE_UP,dragEnd);
}
private function dragBegin(event:MouseEvent):void{
beginLocation=new Point();
beginLocation.x=event.target.x;
beginLocation.y=event.target.y;
//event.target.
//event.target.
event.target.startDrag();
event.target.filters=[new DropShadowFilter()];
}
private function dragEnd(event:MouseEvent):void{
event.target.stopDrag();
event.target.filters=null;
event.target.x=beginLocation.x;
event.target.y=beginLocation.y;
}
private function drawBegin(event:MouseEvent):void{
graphics.moveTo(mouseX,mouseY);
_drawing=true;
}
private function drawing(event:MouseEvent):void{
if(_drawing)
{
graphics.lineTo(mouseX,mouseY);
}
}
private function drawEnd(event:MouseEvent):void{
_drawing=false;
}
}
}