Flash AS教程_屏蔽子级光标响应,节省cpu消耗的测试源码教程
昨晚老衰在群里聊起了光标响应对cpu消耗的问题,也介绍了他的思路,所以今晚就写了一个用DisplayObjectContainer的getObjectsUnderPoint方法的方案. 写的粗糙,请诸位大大指导.
闲话少说,上代码.
package com.aaaqe.common.utils.mouseProxy
{
import flash.events.IEventDispatcher;
/**
* 需要被代理鼠标事件的接口
* @author 汪汪
*
*/
public interface INeedMouseProxy extends IEventDispatcher
{
/**
* 设置鼠标是否响应.
* @return
*
*/
function get enabled():Boolean;
function set enabled(value:Boolean):void;
}
}
package com.aaaqe.common.utils.mouseProxy
{
import flash.display.InteractiveObject;
import flash.events.MouseEvent;
/**
* 代理的鼠标事件
* @author 汪汪
*
*/
public class ProxyMouseEvnet extends MouseEvent
{
public static const PROXY_CLICK:String = "proxyClick";
public static const PROXY_MOUSEDOWN:String = "proxyMousedown";
public static const PROXY_MOUSEUP:String = "proxyMouseup";
public static const PROXY_MOUSEOVER:String = "proxyMouseover";
public static const PROXY_MOUSEOUT:String = "proxyMouseout";
public static const PROXY_ROLLOVER:String = "proxyRollover";
public static const PROXY_ROLLOUT:String = "proxyRollout";
public function ProxyMouseEvnet(type:String, bubbles:Boolean=true, cancelable:Boolean=false, localX:Number=NaN, localY:Number=NaN, relatedObject:InteractiveObject=null, ctrlKey:Boolean=false, altKey:Boolean=false, shiftKey:Boolean=false, buttonDown:Boolean=false, delta:int=0)
{
super(type, bubbles, cancelable, localX, localY, relatedObject, ctrlKey, altKey, shiftKey, buttonDown, delta);
}
/**
* 复制一个正常鼠标事件
* @param event 鼠标事件
* @param relatedObject 这个参数我不理解在MouseEvent里究竟怎么用,暂时先放到这里.
* 希望看到的达人来教教我.多谢.
* @return
*
*/
public static function copyMouseEvent(event:MouseEvent, relatedObject:InteractiveObject):ProxyMouseEvnet
{
var typeName:String;
var evt:ProxyMouseEvnet;
if(( typeName =ProxyMouseEvnet["PROXY_" + event.type.toLocaleUpperCase()]))
{
with(event)
evt = new ProxyMouseEvnet(typeName, bubbles, cancelable, localX, localY,relatedObject, ctrlKey, altKey, shiftKey, buttonDown, delta);
}
return evt;
}
}
}package com.aaaqe.common.utils.mouseProxy
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.InteractiveObject;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.geom.Point;
/**
* 光标代理的静态工具类.
* @author 汪汪
*
*/
public class MouseProxyUtil
{
/**
* 代理被取消鼠标响应对象重新派发事件
* @param evt 一个displayObjectContianer的鼠标事件
* @param parent 非例外的话,parent应该是evt的响应者.
* @return 如果此事件归属一个INeedMouseProxy对象派发的话就返回true,反正则是false.
*
*/
public static function dispatchProxyMouseEvent(evt:MouseEvent,parent:DisplayObjectContainer):Boolean
{
var children:Array;
var needMouseProxy:INeedMouseProxy;
var child:DisplayObject;
var pme:ProxyMouseEvnet;
children = parent.getObjectsUnderPoint( new Point(parent.mouseX,parent.mouseY));
while(children.length)
{
// *** 据本人暂时不可靠观察发现 -- 舞台创建绘制的话,底层会是shape,而代码在sprite上绘制的话,
// *** 第一个对象就是Sprite,这点是舞台的Sprite和脚本的不同.
// *** 然后就是getObjectsUnderPoint得到的数组最后一个就是最顶上的显示对象,
// *** 所以如果child是Shape的话可以用child.parent来得到可以用来正确判断的对象.
child = children.pop();
if(child is Shape)
child = child.parent;
if((needMouseProxy = child as INeedMouseProxy))
{
pme = ProxyMouseEvnet.copyMouseEvent(evt, null);
if(pme)
needMouseProxy.dispatchEvent(pme);
else
{
return false;
}
return true;
}
// *** 如果上层是InteractiveObject,且没禁止鼠标事件,则应当停止检测.
// *** 这个检测在例如自定义光标的情况下有用.
else if( InteractiveObject(child).mouseEnabled)
{
return false;
}
}
return false;
}
}
}package ui
{
import com.aaaqe.common.utils.mouseProxy.ProxyMouseEvnet;
import flash.display.Sprite;
import flash.events.MouseEvent;
import com.aaaqe.common.utils.mouseProxy.INeedMouseProxy;
[Event(type="proxyClick",name="com.aaaqe.common.utils.mouseProxy.ProxyMouseEvent")]
[Event(type="proxyMousedown",name="com.aaaqe.common.utils.mouseProxy.ProxyMouseEvent")]
[Event(type="proxyMouseup",name="com.aaaqe.common.utils.mouseProxy.ProxyMouseEvent")]
[Event(type="proxyRollover",name="com.aaaqe.common.utils.mouseProxy.ProxyMouseEvent")]
[Event(type="proxyRollout",name="com.aaaqe.common.utils.mouseProxy.ProxyMouseEvent")]
public class MouseSprite extends Sprite implements INeedMouseProxy
{
public function MouseSprite()
{
super();
init();
}
private function init():void
{
enabled = false;
draw();
addEventListener(ProxyMouseEvnet.PROXY_CLICK, click);
}
private function click(evt:MouseEvent):void
{
trace(evt.type, evt.currentTarget, evt.currentTarget == evt.target);
}
private var _enabled:Boolean;
public function get enabled():Boolean
{
return _enabled;
}
public function set enabled(value:Boolean):void
{
mouseChildren = mouseEnabled = _enabled = value;
}
public function draw():void
{
with(graphics)
{
beginFill(Math.random() * 0xFFFFFF);
drawRect(0,0,Math.random() * 20 + 10 , Math.random() * 20 + 10);
endFill();
}
}
}
}package
{
import com.aaaqe.common.utils.mouseProxy.INeedMouseProxy;
import com.aaaqe.common.utils.mouseProxy.MouseProxyUtil;
import com.aaaqe.common.utils.mouseProxy.ProxyMouseEvnet;
import flash.display.DisplayObject;
import flash.display.InteractiveObject;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import ui.MouseSprite;
import view.Container;
[SWF(height=700,width=998,frameRate=50)]
public class MouseEnabledTest extends SimpleSprite
{
public function MouseEnabledTest()
{
createChildren();
addEvents();
}
private var container:Container;
private function createChildren():void
{
container = new Container();
addChild(container);
}
private function addEvents():void
{
addEventListener(MouseEvent.CLICK, click);
addEventListener(MouseEvent.ROLL_OVER ,rollOver);
addEventListener(MouseEvent.MOUSE_OUT,mouseOut);
}
private function click(evt:MouseEvent):void
{
MouseProxyUtil.dispatchProxyMouseEvent(evt,container);
}
private function rollOver(evt:MouseEvent):void
{
MouseProxyUtil.dispatchProxyMouseEvent(evt,container);
}
private function mouseOut(evt:MouseEvent):void
{
MouseProxyUtil.dispatchProxyMouseEvent(evt,container);
}
}
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |