【AS3代码】小球拖拽(MouseEvent的方式 和 startDrag的方式)

package
{
    import com.ui.Ball;
    import com.ui.Ship;
    
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class Main extends Sprite
    {    
        private var ball:Ball;
        public function Main():void
        {
            init();
        }
        private function init():void
        {
            ball = new Ball();
            ball.x = 100;
            ball.y = 100;
            this.addChild(ball);
            ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        }
        private function onMouseDown(event:MouseEvent):void
        {
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }
        private function onMouseUp(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }
        private function onMouseMove(event:MouseEvent):void
        {
            ball.x = this.mouseX + (this.mouseX - ball.x);
            ball.y = this.mouseY + (this.mouseY - ball.y);
        }
    }
}

 

 package

{
    import com.ui.Ball;
    import com.ui.Ship;
    
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class Main extends Sprite
    {    
        private var ball:Ball;
        public function Main():void
        {
            init();
        }
        private function init():void
        {
            ball = new Ball();
            ball.x = 100;
            ball.y = 100;
            this.addChild(ball);
            ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        }
        private function onMouseDown(event:MouseEvent):void
        {
            ball.startDrag();
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }
        private function onMouseUp(event:MouseEvent):void
        {
            ball.stopDrag();
        }
    }
}
posted @ 2012-07-11 10:39  王翔(kingfly)  阅读(2586)  评论(0编辑  收藏  举报