Actionscript 3.0 编程实例三 鼠标draw and drag (写给初学的朋友)

Flash拖拽与绘图示例
本文提供了一个使用Flash实现的拖拽与绘图功能示例代码。该示例展示了如何通过监听鼠标事件实现对象的拖拽及绘制线条,并在鼠标超出播放器范围时返回原点以修复已知缺陷。
代码可直接运行,  拖动有一点缺陷。 鼠标超出播放器释放后 会回不到原点。。。欢迎优化。

/**
* 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 beginLocationoint;
                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;
        }
        }
}

转载于:https://www.cnblogs.com/zhegebucuo/archive/2009/03/24/1420632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值