在AS3中最容易实现onReleaseOutside的? [英] Easiest implementation of onReleaseOutside in AS3?

查看:653
本文介绍了在AS3中最容易实现onReleaseOutside的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个较长时间的ActionScript 2的用户,现在开始使用ActionScript 3的一件事,我缺少的是一种简单的方法来复制AS2的MovieClip.onReleaseOutside的功能。它几乎总是必要实施本次活动,否则你得到像闪光灯认为你的鼠标是下来的时候真的很有趣了错误。

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:

  • 这其中触发onReleaseOutside即使是在preSS没有相应的事件。
  • 这其中似乎很低效的,你'会添加和每次鼠标点击您的应用程序内的任何地方随时删除事件侦听器。
  • 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忘了这个改写动作脚本的时候?

There has to be an easier way, don't tell me Adobe forgot about this when rewriting Actionscript?

例AS2 code:

// 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处理程序,如果你pressed向下squre,拖着鼠标在它之外,和释放鼠标,那么方不会取消旋转,而且似乎被卡住了。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆