热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在AS3中最容易实现onReleaseOutside?-EasiestimplementationofonReleaseOutsideinAS3?

Imalong-timeActionScript2user,nowgettingstartedwithActionScript3.TheonethingImmiss

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.

我是一个长期的ActionScript 2用户,现在开始使用ActionScript 3.我缺少的一件事是复制AS2的MovieClip.onReleaseOutside功能的简单方法。几乎总是有必要实现这个事件,否则你会得到一些有趣的错误,比如flash认为你的鼠标已经关闭了。

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:

根据AS2到AS3迁移指南,我应该为此使用flash.display.InteractiveObject.setCapture(),但据我所知,它不存在。我猜这个文件已经过时或不正确。我在网上发现了一些关于如何复制这个功能的帖子,但是他们有自己的问题:

  • This one triggers onReleaseOutside even if there was no corresponding onPress event.
  • 即使没有相应的onPress事件,也会触发onReleaseOutside。

  • This one seems very inefficient, you'll add and remove an event listener every time the mouse is clicked anywhere inside your app.
  • 这个效率似乎非常低效,每次在应用程序内的任何位置单击鼠标时,您都会添加和删除事件侦听器。

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

必须有一个更简单的方法,不要告诉我Adobe在重写ActionScript时忘了这个吗?

Example AS2 code:

示例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;
}

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.

没有onReleaseOutside处理程序,如果你按下squre,将鼠标拖到它外面,然后松开鼠标,那么方块就不会旋转,并且看起来卡住了。

3 个解决方案

#1


10  

Simple and foolproof:

简单而万无一失:

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 );
}

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).

如果您不关心onRelease和onReleaseOutside之间的区别(例如,使用可拖动的项目),您可以跳过按钮本身的鼠标向上监听器(此处用星号表示)。

#2


3  

root.addEventListener(MouseEvent.UP, onMouseReleaseOutside);

You define onMouseReleaseOutside of course. Basically any MouseEvent.UP (a mouse release) that happens outside of your button (or mc) will hit the stage instead of your button. This is the way i usually catch it.

你定义onMouseReleaseOutside当然。基本上任何在按钮(或mc)之外发生的MouseEvent.UP(鼠标释放)都会触发舞台而不是按钮。这是我通常捕捉它的方式。

#3


3  

Have you looked at this event:

你看过这个事件了吗:

flash.events.Event.MOUSE_LEAVE



From the documentation:

从文档:

Dispatched by the Stage object when the mouse pointer moves out of the stage area. The Event.MOUSE_LEAVE constant defines the value of the type property of a mouseLeave event object.

当鼠标指针移出舞台区域时由Stage对象调度。 Event.MOUSE_LEAVE常量定义mouseLeave事件对象的type属性的值。

It will solve your problem if you are only interested whether the user's mouse if off the stage instead of just outside that particular MovieClip.

如果您只关心用户的鼠标是否离开舞台而不是仅仅在特定的MovieClip之外,它将解决您的问题。


推荐阅读
author-avatar
mobiledu2502885243
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有