在 AS3 中最简单的 onReleaseOutside 实现? [英] Easiest implementation of onReleaseOutside in AS3?
问题描述
我是 ActionScript 2 的长期用户,现在开始使用 ActionScript 3.我缺少的一件事是复制 AS2 的 MovieClip.onReleaseOutside 功能的简单方法.几乎总是有必要实现这个事件,否则你会得到一些有趣的错误,比如 flash 认为你的鼠标在真正启动时已经关闭.
I'm a long-time ActionScript 2 user, now getting started with ActionScript 3. The one thing I'm missing is an easy way to duplicate the functionality of AS2's MovieClip.onReleaseOutside. It is almost always necessary to implement this event, otherwise you get funny bugs like flash thinks your mouse is down when really it's up.
根据 AS2 到 AS3 迁移指南,我'我应该为此使用 flash.display.InteractiveObject.setCapture()
,但是据我所知它并不存在.我猜这个文件已经过时或不正确.我在网上找到了一些关于如何复制此功能的帖子,但它们都有自己的问题:
According to the AS2 to AS3 Migration Guide, I'm supposed to use flash.display.InteractiveObject.setCapture()
for this, however it does not exist as far as I can tell. I guess this document is out of date or incorrect. I've found a few posts on the web about how to duplicate this functionality, but they either have their own problems:
- This one triggers onReleaseOutside even if there was no corresponding onPress event.
- This one seems very inefficient, you'll add and remove an event listener every time the mouse is clicked anywhere inside your app.
必须有更简单的方法,难道 Adobe 在重写 Actionscript 时忘记了这一点?
There has to be an easier way, don't tell me Adobe forgot about this when rewriting Actionscript?
示例 AS2 代码:
// Assume myMC is a simple square or something on the stage
myMC.onPress = function() {
this._rotation = 45;
}
myMC.onRelease = myMC.onReleaseOutside = function() {
this._rotation = 0;
}
如果没有 onReleaseOutside 处理程序,如果您按下方块,将鼠标拖到方块外面,然后释放鼠标,那么方块将不会停止旋转,并且看起来像是被卡住了.
Without the onReleaseOutside handler, if you pressed down on the squre, dragged your mouse outside of it, and released the mouse, then the square would not un-rotate, and appear to be stuck.
推荐答案
简单又万无一失:
button.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
button.addEventListener( MouseEvent.MOUSE_UP, buttonMouseUpHandler ); // *
function mouseDownHandler( event : MouseEvent ) : void {
trace( "onPress" );
// this will catch the event anywhere
event.target.stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
}
function buttonMouseUpHandler( event : MouseEvent ) : void {
trace( "onRelease" );
// don't bubble up, which would trigger the mouse up on the stage
event.stopImmediatePropagation( );
}
function mouseUpHandler( event : MouseEvent ) : void {
trace( "onReleaseOutside" );
event.target.removeEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
}
如果您不关心 onRelease 和 onReleaseOutside 之间的区别(例如对于可拖动项目),您可以跳过按钮本身的鼠标向上监听器(此处用星号注释).
If you don't care about the difference between onRelease and onReleaseOutside (for example with draggable items) you an skip the mouse up listener on the button itself (commented here with an asterisk).
这篇关于在 AS3 中最简单的 onReleaseOutside 实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!