当前位置:软件学习 > Flash >>

如何创建可拖动的mc?

如何创建可拖动的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;
    }
  }
}

补充:flash教程,As3.0
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,