Flash/Flex学习笔记(13):对象拖动(startDrag/stopDrag)

Flash中只有影片MovieClip(准确的讲是Sprite)可以调用startDrag,endDrag,创建对象拖动最简单的办法只要调用这二个方法即可

view source

print?

01
myobj.addEventListener(MouseEvent.MOUSE_DOWN,pickup);

02
myobj.addEventListener(MouseEvent.MOUSE_UP,place);

03

04
function pickup(e:MouseEvent ):void {   

05
//trace("鼠标按下");    

06
e.target.startDrag();   

07
}

08

09
function place(e:MouseEvent):void {

10
//trace("鼠标抬起");    

11
e.target.stopDrag();

12
}

其中myobj是舞台上的随便一个MovieClip实例

startDarg还能在拖动时,设定拖动的边界:

view source

print?

01
var ball:Ball=new Ball(30,Math.random()*0xffffff);

02

03
var posX:Number =  stage.stageWidth /2;

04
var posY:Number =  stage.stageHeight/2;

05
var rectSize:Number = 200;

06

07
ball.x = posX;

08
ball.y = posY;

09

10
addChild(ball);

11

12
ball.addEventListener(MouseEvent.MOUSE_OVER,function(){Mouse.cursor = MouseCursor.HAND });

13
ball.addEventListener(MouseEvent.MOUSE_OUT,function(){Mouse.cursor = MouseCursor.AUTO });

14
ball.addEventListener(MouseEvent.MOUSE_DOWN,MouseDownHandler);

15

16
function MouseDownHandler(e:MouseEvent) {

17
stage.addEventListener(MouseEvent.MOUSE_UP, MouseUpHandler);

18
//第一参数如果为true,则表示拖动时,鼠标所在点自动对齐对象中心--即所谓的锁定中心

19
ball.startDrag(true,new Rectangle(posX-rectSize/2, posY-rectSize/2, rectSize, rectSize));

20
//画出边界,方便更直观显示

21
graphics.clear();

22
graphics.lineStyle(1);

23
graphics.drawRect(posX-rectSize/2, posY-rectSize/2, rectSize, rectSize);

24
}

25

26
function MouseUpHandler(e:MouseEvent) {

27
ball.stopDrag();

28
}

下面再来一个稍复杂一点的示例:

view source

print?

01
R1.addEventListener(MouseEvent.MOUSE_DOWN,MouseDownHandler);

02
R1.addEventListener(MouseEvent.MOUSE_UP,MouseUpHandler);

03
R2.addEventListener(MouseEvent.MOUSE_DOWN,MouseDownHandler);

04
R2.addEventListener(MouseEvent.MOUSE_UP,MouseUpHandler);

05
R3.addEventListener(MouseEvent.MOUSE_DOWN,MouseDownHandler);

06
R3.addEventListener(MouseEvent.MOUSE_UP,MouseUpHandler);

07

08
//trace(numChildren);//当前舞台上的元素总数

09

10
var _originPoint:Point = new Point();

11

12
//拖动开始时

13
function MouseDownHandler(e:MouseEvent) {

14
//trace(e);

15

16
//保存原始位置(拖动完成时恢复用)  

17
_originPoint.x=e.target.x;

18
_originPoint.y=e.target.y;

19

20
e.target.startDrag();

21

22
setChildIndex(DisplayObject(e.target), numChildren-1);//设置拖动对象的zIndex,否则有可能在拖动过程中被其它组件挡住

23

24
//拖动时显示阴影

25
var _shadow:DropShadowFilter = new DropShadowFilter();

26
_shadow.distance=10;

27
_shadow.alpha=0.5;

28
_shadow.color = 0xFFFFFF;

29
e.target.filters=[_shadow];

30

31
Mouse.cursor = MouseCursor.HAND;

32

33
}

34

35
//拖动完成时

36
function MouseUpHandler(e:MouseEvent) {

37
e.target.stopDrag();

38
e.target.filters=null;

39

40
if (e.target.dropTarget is Shape){

41
e.target.dropTarget.transform.colorTransform=e.target.transform.colorTransform; //将目标对象颜色设置为与源对象一致

42
}   

43
//还原初始位置

44
e.target.x = _originPoint.x;

45
e.target.y = _originPoint.y;

46

47
Mouse.cursor = MouseCursor.ARROW;

48
}

源文件下载:http://cid-2959920b8267aaca.skydrive.live.com/self.aspx/Flash/DragObject.rar

posted @ 2010-11-22 16:30  模西的哥哥  阅读(552)  评论(0编辑  收藏  举报