一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

如何创建可拖动的mc?

时间:2022-07-02 17:05:37 编辑:袖梨 来源:一聚教程网

如何创建可拖动的mc?下面就来看看利用as 3.0 创建可拖动的mc的一代码,注明本文来自网络.

package com.sxl001{
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  public class DragMc extends Sprite {
    private var circle:Sprite;
    private var begin:Boolean;
    private var dx:Number;//用于计算MouseX与圆的x位置差的变量
    private var dy:Number;//用于计算MouseY与圆的y位置差
    public function DragMc() {
      begin=false;
      init();
    }
    private function init() {
      circle=new Sprite();
      addChild(circle);
      circle.graphics.beginFill(0xFF0000);
      circle.graphics.drawCircle(0,0,20);
      circle.graphics.endFill();
      circle.x=stage.stageWidth/2-circle.width/2;
      circle.y=stage.stageHeight/2-circle.height/2;
      circle.addEventListener(MouseEvent.MOUSE_DOWN,onMD);
      circle.addEventListener(MouseEvent.MOUSE_MOVE,onMM);
      circle.addEventListener(MouseEvent.MOUSE_UP,onMU);
    }
    private function onMD(ev:MouseEvent) {
      dx=stage.mouseX-circle.x;//计算MouseX与圆的x位置差
      dy=stage.mouseY-circle.y;//计算MouseY与圆的y位置差
      begin=true;
    }
    private function onMM(ev:MouseEvent) {
      if (begin) {//圆跟随Mouse
        circle.x=stage.mouseX-dx;
        circle.y=stage.mouseY-dy;
      }
    }
    private function onMU(ev:MouseEvent) {
      begin=false;
    }
  }
}

热门栏目