AS3:当鼠标离开舞台时,MOUSE_OUT 不会触发
我正在开发一个网站,其导航项目从上到下覆盖整个舞台(请参见下面的更改图像),用户可以很容易地用鼠标退出舞台,而不触发 MouseEvent.MOUSE_OUT
MouseEvent.MOUSE_OUT代码>“关闭”表示导航项所需的事件。
我是否应该使用 Event.MOUSE_LEAVE
来检测鼠标何时离开舞台,并关闭任何启用的导航项?这就是我一直在尝试做的事情,但无法从我的听众那里获得任何输出。有什么想法吗?
替代文本 http://marcysutton.com/blog/wp- content/uploads/2010/01/redpropeller.png
对于 Flash IDE 中与影片剪辑关联的类,这是注册 Event.MOUSE_LEAVE
侦听器的正确语法吗?无论我做什么,它似乎都没有做任何事情。我是否必须将电影嵌入浏览器才能触发事件?
this.stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
这是我的 MainNav.as 课程:
package com.redpropeller {
import com.greensock.*;
import com.greensock.plugins.*;
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class MainNav extends MovieClip { // MainNav is a movieclip in the IDE
public var colors:Array;
public function MainNav():void {
colors = new Array(0xee3124, 0xc72a1f, 0xa62c24, 0x912923, 0x7e221c);
TweenPlugin.activate([TintPlugin]);
// trying to target stage through this object
this.stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
for(var i:Number=0; i<this.numChildren; i++){
var n = this.getChildAt(i);
n.useHandCursor = true;
n.buttonMode = true;
n.addEventListener(MouseEvent.MOUSE_OVER, navBtnOn);
n.addEventListener(MouseEvent.MOUSE_OUT, navBtnOff);
}
}
public function mouseLeaveListener(e:Event):void {
trace('mouseleave'); // nothing ever happens
}
private function navBtnOn(e:MouseEvent):void {
TweenLite.to(e.currentTarget.bar_mc, 0.01, {tint:0x333333});
}
private function navBtnOff(e:MouseEvent):void {
TweenLite.to(e.currentTarget.bar_mc, 0.01,
{tint:uint(colors[this.getChildIndex(MovieClip(e.currentTarget))])});
// changes color back to specific tint
}
}
}
I am developing a website with nav items that cover the whole stage from top to bottom (see altered image below) and it is pretty easy for the user to exit the stage with their mouse, not triggering the MouseEvent.MOUSE_OUT
events required to "turn off" said nav items.
Should I be using Event.MOUSE_LEAVE
to detect when the mouse has left the stage, and turn off any enabled nav items? That is what I been trying to do, but have had trouble getting any output from my listener. Any ideas?
alt text http://marcysutton.com/blog/wp-content/uploads/2010/01/redpropeller.png
For a class associated with a movieclip in the Flash IDE, is this the correct syntax for registering an Event.MOUSE_LEAVE
listener? It doesn't seem to do anything no matter what I do. Is it a case where I have to embed the movie in a browser for the event to fire?
this.stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
Here is my MainNav.as class:
package com.redpropeller {
import com.greensock.*;
import com.greensock.plugins.*;
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class MainNav extends MovieClip { // MainNav is a movieclip in the IDE
public var colors:Array;
public function MainNav():void {
colors = new Array(0xee3124, 0xc72a1f, 0xa62c24, 0x912923, 0x7e221c);
TweenPlugin.activate([TintPlugin]);
// trying to target stage through this object
this.stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveListener);
for(var i:Number=0; i<this.numChildren; i++){
var n = this.getChildAt(i);
n.useHandCursor = true;
n.buttonMode = true;
n.addEventListener(MouseEvent.MOUSE_OVER, navBtnOn);
n.addEventListener(MouseEvent.MOUSE_OUT, navBtnOff);
}
}
public function mouseLeaveListener(e:Event):void {
trace('mouseleave'); // nothing ever happens
}
private function navBtnOn(e:MouseEvent):void {
TweenLite.to(e.currentTarget.bar_mc, 0.01, {tint:0x333333});
}
private function navBtnOff(e:MouseEvent):void {
TweenLite.to(e.currentTarget.bar_mc, 0.01,
{tint:uint(colors[this.getChildIndex(MovieClip(e.currentTarget))])});
// changes color back to specific tint
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案:Event.MOUSE_LEAVE 在 IDE 中不会触发。当电影嵌入 HTML 页面时,它工作得很好。感谢您的帮助!
Answer: Event.MOUSE_LEAVE does not fire in the IDE. It works fine when the movie is embedded in an HTML page. Thanks for your help!
您正在尝试在构造函数中创建 MOUSE_LEAVE 的事件侦听器。如果尚未通过 addChild() 方法将 MainNav 添加到舞台,则舞台对象可能尚不存在。如果 MainNav 在设计时已经在舞台上,该舞台仍然有可能不会立即可用。对于从 DisplayObject 继承的类(MovieClip、Sprite 等),我在构造函数中只做了一件事:设置一个 Event.ADDED_TO_STAGE 侦听器。当对象已通过父容器中的 addChild() 添加到舞台的显示堆栈时,或者对象在设计时已位于舞台上时,该侦听器会触发 init() 方法。当我的 init() 方法被调用时,我知道 stage 属性将可用。
在您的构造函数中,实例中可能尚不存在阶段,但您应该会收到运行时错误。但是,您提前使用了“this”关键字。当您在继承自 Object 的类中使用“this”时(您的类通过 MovieClip->DisplayObject->EventDispatcher->Object 进行操作),如果该属性不存在,编译器不会抛出错误,因为它会尝试创建该属性作为“this”的成员。发生这种情况是因为 Object 类是动态的,这意味着可以随时创建新属性,而无需在标头中将它们声明为类变量 - 您只需在使用该动态属性时使用“this”关键字即可。当您使用 this.stage 时,如果 stage 不存在,该类会为您创建属性 stage。然而,这不是您想要的阶段,因此侦听器只是附加到一个不执行任何操作的空对象。尝试在引用阶段时删除“this”,我相信您会在某个时候看到错误。一般来说,对属性使用“this”并不是一个好习惯,因为编译器会忽略该属性的类型错误。
您在上面的一条评论中提到 MOUSE_LEAVE 在 IDE 中不起作用,但我在 CS4 中对此进行了测试,它确实有效。与浏览器的 Flash Player 插件相比,您可能会发现 IDE 的 Flash Player 存在性能差异。在某些情况下,如果 SWF 加载很快并且舞台立即可用,则舞台事件侦听器将从构造函数中工作,但它并不可靠。将该侦听器移至 ADDED_TO_STAGE 事件之后调用的 init() 方法,并且不要使用“this”关键字。
You're trying to create the event listener for MOUSE_LEAVE in the constructor. It's possible that the stage object does not exist yet if MainNav has not been added to the stage via an addChild() method. If MainNav is already on the stage at design time, it's still possible that stage would not be available immediately. For classes that inherit from the DisplayObject (MovieClip, Sprite, etc.), I do only one thing in the constructor: set up an Event.ADDED_TO_STAGE listener. That listener triggers an init() method when the object has been added to the stage's display stack via addChild() from a parent container, or if the object is already on the stage at design time. When my init() method is called, I know that the stage property will be available.
In your constructor, stage may not exist in the instance yet, but you should get a runtime error. However, you're using the 'this' keyword ahead of stage. When you use 'this' in a class that inherits from Object (your class does via MovieClip->DisplayObject->EventDispatcher->Object), the compiler won't throw an error if the property does not exist because it attempts to create that property as a member of 'this'. This happens because the Object class is dynamic, meaning that new properties can be created at any time without having to declare them as class variables in the header -- you just have to use the 'this' keyword when using that dynamic property. When you use this.stage, if stage does not exist the class creates the property stage for you. However, this is not the stage you want, so the listener is just getting attached to an empty object that doesn't do anything. Try removing 'this' when referencing stage and I'm sure you'll see the error at some point. In general, it's not good practice to use 'this' for properties, since the compiler will ignore type errors for that property.
You mention in one comment above that MOUSE_LEAVE does not work in the IDE, but I tested this from CS4 and it does work. You may be witnessing a performance difference from the IDE's Flash Player compared to the browser's Flash Player plugin. In some cases your stage event listener will work from the constructor if the SWF loads quickly and stage is available immediately, but it's not reliable. Move that listener to an init() method that is called after the ADDED_TO_STAGE event and don't use the 'this' keyword.