var moveSteps:int = 20; var drag_mode:int = 0; var main_x, main_y:Number; addEventListener(Event.ENTER_FRAME, getMouseXY); startDrag_btn.addEventListener(MouseEvent.CLICK, dragStart); moveTo_btn.addEventListener(MouseEvent.CLICK, moveStart); stop_btn.addEventListener(MouseEvent.CLICK, dragStop); stop(); function getMouseXY(event:Event) { var mx:Number = mouseX; var my:Number = mouseY; if ( mx < 50 ) { mx = 50; } if ( mx > 590 ) { mx = 590; } if ( my < 50 ) { my = 50; } if ( my > 350 ) { my = 350; } main_x = mx; main_y = my; if ( drag_mode > 0 ) { mouse_txt.text = "主角座標(" + main_x + " ," + main_y + ")"; } else { mouse_txt.text = ""; } } function dragStart(event:MouseEvent) { actor_mc.gotoAndStop(1); if ( drag_mode == 2 ) { actor_mc.removeEventListener(Event.ENTER_FRAME, moveTo); } actor_mc.startDrag(true, new Rectangle(50,50,540,300)); drag_mode = 1; } function moveStart(event:MouseEvent) { actor_mc.gotoAndStop(2); if ( drag_mode == 1 ) { actor_mc.stopDrag(); } actor_mc.addEventListener(Event.ENTER_FRAME, moveTo); drag_mode = 2; } function moveTo(event:Event) { var dx:Number = (actor_mc.x - main_x) / moveSteps; var dy:Number = (actor_mc.y - main_y) / moveSteps; actor_mc.x -= dx; actor_mc.y -= dy; } function dragStop(event:MouseEvent) { if ( drag_mode == 1 ) { actor_mc.stopDrag(); } else if ( drag_mode == 2 ) { actor_mc.removeEventListener(Event.ENTER_FRAME, moveTo); } drag_mode = 0; }