Flash/Flex学习笔记(30):不用startDrag和stopDrag的对象拖动

对于从Sprite类继承来的对象,要实现拖放当然是Flash/Flex学习笔记(13):对象拖动(startDrag/stopDrag) 里讲的方法最方便,但是对于不是从Sprite类继承得来的对象,这startDrag/stopDrag是不能用的,这时候只能采用最通常用做法:利用Mouse_Down,Mouse_UP,Mouse_Move事件来处理

注意:对象的Mouse_Move事件,只有当鼠标在对象上时才能被监听,如果用户鼠标移动过快,超出了对象的范围,该事件就不起作用了,所以监听Mouse_Move事件时,应该监听stage对象的Mouse_Move事件,而非对象本身的(详情见下面的代码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.ui.MouseCursor;
    import flash.ui.Mouse;
 
    public class MouseMoveDrag extends Sprite {
 
        private var offsetX:Number;
        private var offsetY:Number;
        private var arrBalls:Array;
        private var _draggedBall:Ball;
 
 
        public function MouseMoveDrag():void {
 
            arrBalls=new Array(7);
 
            for (var i:uint=0; i<arrBalls.length; i++) {
                arrBalls[i]=new Ball(20+Math.random()*30,Math.random()*0xffffff);
                arrBalls[i].x=stage.stageWidth*Math.random();
                arrBalls[i].y=stage.stageHeight*Math.random();
                arrBalls[i].addEventListener(MouseEvent.MOUSE_DOWN, MouseDownHandler);
                arrBalls[i].addEventListener(MouseEvent.MOUSE_UP, MouseUpHandler);
                arrBalls[i].addEventListener(MouseEvent.MOUSE_OVER,MouseOverHandler);
                arrBalls[i].addEventListener(MouseEvent.MOUSE_OUT,MouseOutHandler);
                addChild(arrBalls[i]);
            }
 
        }
 
        function MouseOverHandler(e:MouseEvent):void {
            Mouse.cursor=MouseCursor.BUTTON;
        }
 
        function MouseOutHandler(e:MouseEvent):void {
            Mouse.cursor=MouseCursor.AUTO;         
        }
 
        //开始移动
        function MouseDownHandler(e:MouseEvent):void {
            offsetX=e.localX;
            offsetY=e.localY;          
            _draggedBall = e.target as Ball;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);
        }
 
        //停止拖动
        function MouseUpHandler(e:MouseEvent):void {
            _draggedBall = null;
            //拖动完成后,移除舞台的Mouse_Move监听
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);
        }
 
 
        function stageMouseMoveHandler(e:MouseEvent):void {
            //trace(e.target);
            var _ball:Ball=e.target as Ball;
            if (_ball!=null) {
                //设置新的坐标
                _ball.x=mouseX-offsetX;
                _ball.y=mouseY-offsetY;
            }
            else {
                //trace("您拖得太快了!" + _draggedBall.toString());
                if (_draggedBall!=null){
                    _draggedBall.x=mouseX-offsetX;
                    _draggedBall.y=mouseY-offsetY;
                }
            }
        }
 
    }
}

posted @   菩提树下的杨过  阅读(2766)  评论(1编辑  收藏  举报
编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用
历史上的今天:
2008-04-09 几种常见复合sql查询语句的linq写法[继续补充中]
2008-04-09 [原创]在msmq3.0中使用http协议发送消息
点击右上角即可分享
微信分享提示