我是 ActionScript 2 的长期用户,现在开始使用 ActionScript 3。我缺少的一件事是复制 AS2 的 MovieClip.onReleaseOutside 功能的简单方法。几乎总是有必要实现这个事件,否则你会得到一些有趣的错误,比如 flash 认为你的鼠标在真正启动时已关闭。
根据AS2 to AS3 Migration Guide ,我应该使用 flash.display.InteractiveObject.setCapture()
为此,但据我所知,它不存在。我猜这个文件已经过时或不正确。我在网上找到了一些关于如何复制此功能的帖子,但它们都有自己的问题:
必须有更简单的方法,不要告诉我Adobe在重写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 处理程序,如果您按下方格,将鼠标拖到它的外面,然后松开鼠标,那么方格就不会旋转,并且似乎被卡住了。
最佳答案
简单且万无一失:
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 之间的区别(例如对于可拖动项目),您可以跳过按钮本身上的鼠标向上监听器(此处用星号注释)。
关于flash - 在 AS3 中最简单的 onReleaseOutside 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/255133/