AS3自定义鼠标光标后应注意鼠标事件捕获问题
AS3自定义鼠标光标很容易,在帮助中就有这样的代码:

var cursor:Sprite = new Sprite();
cursor.graphics.beginFill(0x000000);
cursor.graphics.drawCircle(0,0,20);
cursor.graphics.endFill();
addChild(cursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE,redrawCursor);
Mouse.hide();
function redrawCursor(event:MouseEvent):void
{
cursor.x = event.stageX;
cursor.y = event.stageY;
}
cursor.graphics.beginFill(0x000000);
cursor.graphics.drawCircle(0,0,20);
cursor.graphics.endFill();
addChild(cursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE,redrawCursor);
Mouse.hide();
function redrawCursor(event:MouseEvent):void
{
cursor.x = event.stageX;
cursor.y = event.stageY;
}
但我们实际的项目不可能简单到没有其他的影片剪辑,假如出现下面的代码:

var mc:Sprite = new Sprite();
mc.graphics.beginFill(0xFF0000);
mc.graphics.drawRect(200,200,100,100);
mc.graphics.endFill();
addChild(mc);
mc.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
function overHandler(event:MouseEvent):void
{
trace("我不是每次碰到都显示!!");
}
var cursor:Sprite = new Sprite();
cursor.graphics.beginFill(0x000000);
cursor.graphics.drawCircle(0,0,20);
cursor.graphics.endFill();
addChild(cursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE,redrawCursor);
Mouse.hide();
function redrawCursor(event:MouseEvent):void
{
cursor.x = event.stageX;
cursor.y = event.stageY;
}
mc.graphics.beginFill(0xFF0000);
mc.graphics.drawRect(200,200,100,100);
mc.graphics.endFill();
addChild(mc);
mc.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
function overHandler(event:MouseEvent):void
{
trace("我不是每次碰到都显示!!");
}
var cursor:Sprite = new Sprite();
cursor.graphics.beginFill(0x000000);
cursor.graphics.drawCircle(0,0,20);
cursor.graphics.endFill();
addChild(cursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE,redrawCursor);
Mouse.hide();
function redrawCursor(event:MouseEvent):void
{
cursor.x = event.stageX;
cursor.y = event.stageY;
}
上面的代码运行时,如果你鼠标移动的比较慢的话,你会发现trace函数总是不能执行,这是为什么呢?
原因是因为AS3事件的冒泡机制,事件的触发总是从发生事件的最子显示对象(child)开始向它的父显示对象(parent)逐次传递直到stage对象,这样将会发生两种情况:
1. 如果两个显示对象是某个显示对象的子显示对象或孙显示对象或曾孙显示对象...但它们没有父子这样的容器(contains)关系(记住AS3中的父(parent)与子(child)并不是继承关系,而是包含关系(contains)),那么他们两个在某一时刻只有一个能捕获事件。
2.如果某个显示对象A的索引(index,AS1和2中叫深度)比另一个显示对象B大,那么即便B与A处在同样的位置(即它们重叠),像鼠标事件发生在A上了,B就不会发生了,因为鼠标事件首先被A捕获到。
正因为第2种情况,上面的第2段代码鼠标移动的比较慢时trace函数总是不能执行,因为mc显示对象首先添加,它的Index比cursor小。至于快速移动就能执行trace函数是因为cursor显示对象移动迟缓,跟不上鼠标的移动速度,导致鼠标在短时间里可以停留在mc上。
作者:Rick Carter
出处:http://pains.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:
ActionScript
标签:
ActionScript
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库