Posted on 2006-06-14 18:43
这里的黄昏静悄悄 阅读(939)
评论(0) 编辑 收藏 引用 所属分类:
Action Script
/*----------------------------文档说明-------------------------
* Flash中常用一些自制的形状代替鼠标形状进行一些操作,比如模拟画笔
* 用手的形状拖放、模拟颜料桶,这个时候如果不管理好鼠标监听器的注册
* 卸载,以及鼠标的隐藏和显示的话会有很多意向不到的后果出现。
* 这里写了一个监听器的注册管理函数,用来管理鼠标监听器的注册、卸载、显示
* 和隐藏。
* 首先定义了几个要用到的状态码,定义各种Object用于注册监听器,然后用
* RegMouse函数进行注册,该函数接收一个Number类型的参数,也就是要注册的监听器类型
* 该函数首先卸载当前注册的监听器,然后注册新的监听器,如果当前监听器和要注册的新监听器一样的话
* 就卸载当前监听器,并显示鼠标的默认状态。
* 本例子主要是在制作一个Flash模拟Windows画图板的时候写的,下面是其中的部分代码。
*----------------------------------------------------------------------*/
/*-------------- 鼠标状态定义------------
* */var _cursorFlag:Number = 0;// 记录当前注册的鼠标监听器状态。var _cursorObj:Object = newObject();
_cursorObj.def = 0;
_cursorObj.drag = 1;
_cursorObj.select = 2;
_cursorObj.tong = 3;
_cursorObj.pip = 4;
/*----------状态码---------------------
* 0:默认状态,无任何形状,只能切换到其他状态后进行操作
* 1:手的形状,可以从物品栏中拖物品
* 2:选择状态,可以选择操作区中的东西
* 3:油漆桶状态,可以涂色
* 4:吸管状态,设置颜色
*-------------------------------------
*----------------------------------------*/// 按钮事件定义/*--------------- 用于控制菜单消失的鼠标监听器--------
* 当菜单显示出来,并且在菜单之外的地方单击鼠标时,让菜单消失(主要是画图板工具栏上的菜单)*
*--------------------------------------------------------------*/var _mmListen:Object = newObject();
_mmListen.onMouseDown = function():Void{
if(!menu_mc.hitTest(_xmouse, _ymouse))
{
Mouse.addListener(drawObj);
Mouse.removeListener(_mmListen);
menu_mc.gotoAndStop(1);
}};
// 点击颜料桶时注册监听器var dmtong:Object = newObject();
dmtong.onMouseMove = function():Void{
dmtong_mc._x = _xmouse;
dmtong_mc._y = _ymouse;
};
// 吸管工具var dmpip:Object = newObject();
dmpip.onMouseMove = function():Void{
dmpip_mc._x = _xmouse;
dmpip_mc._y = _ymouse;
};
// 选择工具var dmselect:Object = newObject();
dmselect.onMouseMove = function():Void{
dmcursor_mc._x = _xmouse;
dmcursor_mc._y = _ymouse;
};
// 画线工具
line_mc.clear();
var drawObj:Object = newObject();
drawObj.x1 = 0;
drawObj.y1 = 0;
drawObj.x2 = 0;
drawObj.y2 = 0;
drawObj.xx = 0;
drawObj.yy = 0;
drawObj.drawFlag = false;
drawObj.moveFlag = false;
drawObj.startFlag = false;
drawObj.moveMC = newArray();
var plasterFlag:Boolean = false;
drawObj.onMouseDown = function():Void{
// 如果在操作区之外按下鼠标,则返回
if(_xmouse <= 68 || _xmouse >= 400 || _ymouse >= 260 || _ymouse <= 55)
{
return;
}
// 如果当前状态是不是移动选中的物品,则记录按下位置,用来画线
if(drawObj.moveFlag != true)
{
drawObj.x1 = _xmouse;
drawObj.y1 = _ymouse;
drawObj.drawFlag = true;
}
else
{
drawObj.xx = _xmouse;
drawObj.yy = _ymouse;
drawObj.moveMC = newArray();
selectRect();
}
drawObj.startFlag = true;
};
drawObj.onMouseUp = function():Void{
if(drawObj.moveFlag != true)
{
drawObj.x2 = _xmouse;
drawObj.y2 = _ymouse;
if(drawObj.x1 == _xmouse && drawObj.y1 == _ymouse)
{
line_mc.clear();
}
else
{
dmcursor_mc.nextFrame();
}
drawObj.drawFlag = false;
}
drawObj.startFlag = false;
trace("x1 = " + drawObj.x1);
};
// 单鼠标在选中区域内时,变成移动图标,否则变成选择图标。
line_mc.onEnterFrame = function():Void{
if(_cursorFlag == _cursorObj.select && drawObj.x1 != drawObj.x2 && drawObj.y1 != drawObj.y2 && drawObj.startFlag == false)
{
if((_xmouse - drawObj.x1) * (_xmouse - drawObj.x2) < 0 && (_ymouse - drawObj.y1) * (_ymouse - drawObj.y2) < 0)
{
drawObj.moveFlag = true;
drawObj.drawFlag = false;
dmcursor_mc.gotoAndStop(2);
}
else
{
drawObj.moveFlag = false;
drawObj.drawFlag = true;
dmcursor_mc.gotoAndStop(1);
}
}};
drawObj.onMouseMove = function():Void{
trace(drawObj.startFlag);
if(_cursorFlag == _cursorObj.select && drawObj.startFlag == true)
{
if(drawObj.moveFlag == true)
{
// 移动
if(drawObj.x1 + (_xmouse - drawObj.xx) < 65 || drawObj.x1 + (_xmouse - drawObj.xx) > 400 || drawObj.x2 + (_xmouse - drawObj.xx) < 65 || drawObj.x2 + (_xmouse - drawObj.xx) > 400 || drawObj.y1 + (_ymouse - drawObj.yy) < 50 || drawObj.y1 + (_ymouse - drawObj.yy) > 260 || drawObj.y2 + (_ymouse - drawObj.yy) < 50 || drawObj.y2 + (_ymouse - drawObj.yy) > 260)
{
return;
}
for(var mm in drawObj.moveMC)
{
drawObj.moveMC[mm]._x += _xmouse - drawObj.xx;
drawObj.moveMC[mm]._y += _ymouse - drawObj.yy;
drawObj.moveMC[mm].x = drawObj.moveMC[mm]._x;
drawObj.moveMC[mm].y = drawObj.moveMC[mm]._y;
}
drawObj.x1 += _xmouse - drawObj.xx;
drawObj.y1 += _ymouse - drawObj.yy;
drawObj.x2 += _xmouse - drawObj.xx;
drawObj.y2 += _ymouse - drawObj.yy;
drawObj.xx = _xmouse;
drawObj.yy = _ymouse;
}
else
{
drawObj.x2 = _xmouse;
drawObj.y2 = _ymouse;
}
drawRect();
}
else
{
}};
// 手型图标var handObj:Object = newObject();
handObj.onMouseMove = function():Void{
hand_mc._x = _xmouse;
hand_mc._y = _ymouse;
};
hand_btn.onRelease = function():Void{
RegMouse(_cursorObj.drag);
};
// 鼠标监听器注册管理函数function RegMouse(regID:Number):Void{
if(regID < 0 || regID > 4)
{
return;
}
// 清除上次的
switch(_cursorFlag)
{
case _cursorObj.def :
Mouse.hide();
break;
case _cursorObj.drag :
hand_mc._x = hand_mc._y = -500;
Mouse.removeListener(handObj);
break;
case _cursorObj.select :
dmcursor_mc._x = dmcursor_mc._y = -500;
select_mc.gotoAndStop(1);
line_mc.clear();
Mouse.removeListener(drawObj);
Mouse.removeListener(dmselect);
break;
case _cursorObj.tong :
dmtong_mc._x = dmtong_mc._y = -500;
tong_mc.gotoAndStop(1);
Mouse.removeListener(dmtong);
break;
case _cursorObj.pip :
dmpip_mc._x = dmpip_mc._y = -500;
pip_mc.gotoAndStop(1);
Mouse.removeListener(dmpip);
break;
}
if(_cursorFlag == regID)
{
_cursorFlag = 0;
Mouse.show();
return;
}
// 注册新的监听器
switch(regID)
{
case _cursorObj.def :
Mouse.show();
break;
case _cursorObj.drag :
hand_mc._x = _xmouse;
hand_mc._y = _ymouse;
Mouse.addListener(handObj);
break;
case _cursorObj.select :
dmcursor_mc._x = _xmouse;
dmcursor_mc._y = _ymouse;
Mouse.addListener(drawObj);
Mouse.addListener(dmselect);
break;
case _cursorObj.tong :
dmtong_mc._x = _xmouse;
dmtong_mc._y = _ymouse;
Mouse.addListener(dmtong);
break;
case _cursorObj.pip :
dmpip_mc._x = _xmouse;
dmpip_mc._y = _ymouse;
Mouse.addListener(dmpip);
break;
}
_cursorFlag = regID;
}