2007/09/02 | 自定义光标类——CustomCursor
类别(Flash习作) | 评论(1) | 阅读(698) | 发表于 20:20


//功能:设置自定义的光标并在指定的区域或MC上显示
//AS2.0编写

//该类使用单列模式,且只public一个静态方法:setCursor(style:String, targetArr:Array, area)
//用例1:CustomCursor.setCursor("linkname",[mc1,mc2,mc3],{left:0,right:400,top:0,bottom:100}) 将库中链接名为“linkname”的MC作为自定义光标,并作用在mc1,mc2,mc3以及矩形区域(0,0)~(400,100)上
//用例2:CustomCursor.setCursor() 恢复使用默认光标
 压缩包下载

class CustomCursor {
 private var eventMC:MovieClip;
 private var cursorMC:MovieClip;
 private var areaObj;
 private var targetArr:Array;
 private var style:String;
 private var using:Boolean;
 private var onMouseMove:Function;
 private static var instance:CustomCursor;
 //静态方法,创建自定义光标,设置光标样式和显示区域
 public static function setCursor(style:String, targetArr:Array, area) {
  if (instance == null) {
   instance = new CustomCursor();
  }
  instance.setCursorStyle(style);
  instance.setArea(area);
  instance.setTargets(targetArr);
  instance.refurbish();
 }
 //私有构造函数
 private function CustomCursor() {
  this.eventMC = _root.createEmptyMovieClip("CustomCursor_eventMC", 7085);
  this.cursorMC = null;
  this.areaObj = null;
  this.targetArr = null;
  this.style = null;
  this.using = true;
  this.eventMC.parent = this;
  this.eventMC.onMouseMove = function() {
   this.parent.refurbish();
  };
 }
 //设置光标样式
 private function setCursorStyle(value:String) {
  this.style = value;
  if (this.style == null) {
   Mouse.show();
   this.cursorMC.removeMovieClip();
   this.cursorMC = null;
  } else {
   Mouse.hide();
   this.cursorMC = _root.attachMovie(style, "CustomCursor_cursorMC", 7086);
   newLocation();
  }
 }
 private function newLocation() {
  this.cursorMC._x = _root._xmouse;
  this.cursorMC._y = _root._ymouse;
  updateAfterEvent();
 }
 //设置自定义光标的使用区域,obj可以是null,也可以是Object {left:*,right:*,top:*,bottom:*}
 private function setArea(obj) {
  if (obj != null) {
   obj.left = obj.left == null ? 0 : obj.left;
   obj.right = obj.right == null ? Stage.width : obj.right;
   obj.top = obj.top == null ? 0 : obj.top;
   obj.bottom = obj.bottom == null ? Stage.height : obj.bottom;
  }
  this.areaObj = obj;
 }
 //设置自定义光标的使用对象MovieClip
 private function setTargets(arr) {
  this.targetArr = arr;
 }
 //是否显示自定义光标
 private function useStyle(use:Boolean) {
  if (this.using != use) {
   this.cursorMC._visible = this.using=use;
   use ? Mouse.hide() : Mouse.show();
  }
 }
 //刷新光标
 private function refurbish() {
  var area = this.areaObj;
  var arr = this.targetArr;
  //判断光标是否在指定区域内
  var condition1 = area != null && _root._xmouse>area.left && _root._xmouse<area.right && _root._ymouse>area.top && _root._ymouse<area.bottom;
  //如果不在指定区域内,进一步判断光标是否在目标MC上
  if (!condition2) {
   var condition2 = false;
   for (var i in arr) {
    if (arr[i].hitTest(_root._xmouse, _root._ymouse, true)) {
     condition2 = true;
     break;
    }
   }
  }
  //当光标位于设定区域内或是位于目标MC上时,显示自定义光标,否则显示默认光标       
  if (condition1 || condition2) {
   this.useStyle(true);
   newLocation();
  } else {
   this.useStyle(false);
  }
 }
}

0

评论Comments