如何在 AS3 中对计时器事件和鼠标事件使用相同的函数?
所以今天我的问题是我们如何才能让 1 个函数同时由计时器事件和鼠标事件触发?我试图避免仅仅因为我还有一个触发它的鼠标事件而不仅仅是一个计时器事件而必须重新编写/重新命名一个函数。
问题:
我有一个 addThis 气泡淡入,并在按钮翻转时带有图标,addThis 气泡将在计时器完成后淡出,但该气泡中还有一个关闭按钮,需要使用计时器使用的相同功能。 (想要避免编写两个完全相同的函数)
我希望有一个简单的解决方法,例如 event:null 或其他东西。
计时器事件:
private function overShareButton(event:MouseEvent):void
{
shareIconsTimer = new Timer(3000,1);
shareIconsTimer.addEventListener(TimerEvent.TIMER, fadeOutShareIcons);
shareIconsTimer.start();
fadeInShareIcons();
}
关闭按钮事件:
shareIcons.btn_closeIcons.addEventListener(MouseEvent.MOUSE_UP, fadeOutShareIcons);
addThis Bubble 淡出函数:
// Fade out the addThis Icons
private function fadeOutShareIcons(e:TimerEvent):void
{
shareIcons.buttonMode = false;
shareIcons.btn_closeIcons.buttonMode = false;
shareIcons.btn_email.buttonMode = false;
shareIcons.btn_facebook.buttonMode = false;
shareIcons.btn_myspace.buttonMode = false;
shareIcons.btn_digg.buttonMode = false;
shareIcons.btn_delicious.buttonMode = false;
shareIcons.btn_favorites.buttonMode = false;
shareIcons.btn_twitter.buttonMode = false;
shareIcons.btn_google.buttonMode = false;
shareIcons.btn_messenger.buttonMode = false;
shareIcons.btn_stumbleupon.buttonMode = false;
// STOP TIMER & Remove share icon button events...
shareIconsTimer.stop();
shareIconsTimer.removeEventListener(TimerEvent.TIMER, fadeOutShareIcons);
shareIcons.btn_email.removeEventListener (MouseEvent.MOUSE_UP, addThisToEmail);
shareIcons.btn_facebook.removeEventListener (MouseEvent.MOUSE_UP, addThisToFacebook);
shareIcons.btn_myspace.removeEventListener (MouseEvent.MOUSE_UP, addThisToMyspace);
shareIcons.btn_digg.removeEventListener (MouseEvent.MOUSE_UP, addThisToDigg);
shareIcons.btn_delicious.removeEventListener (MouseEvent.MOUSE_UP, addThisToDelicious);
shareIcons.btn_favorites.removeEventListener (MouseEvent.MOUSE_UP, addThisToFavorites);
shareIcons.btn_twitter.removeEventListener (MouseEvent.MOUSE_UP, addThisToTwitter);
shareIcons.btn_google.removeEventListener (MouseEvent.MOUSE_UP, addThisToGoogle);
shareIcons.btn_messenger.removeEventListener (MouseEvent.MOUSE_UP, addThisToLive);
shareIcons.btn_stumbleupon.removeEventListener (MouseEvent.MOUSE_UP, addThisToStumbleupon);
TweenLite.to(shareIcons, .2, {alpha:0});
}
我现在唯一的选择是创建另一个函数来处理关闭按钮 MouseEvent,任何提示/想法表示赞赏! :D
so today my question is how can we have 1 function triggered by both a Timer event and a Mouse event? 'm trying to avoid having to re-write/re-name a function just because I also have a Mouse event that triggers it and not just a Timer event.
Issue:
I have a addThis bubble fade in with icons on a button rollover, the addThis bubble will fade out after the timer is finished, but I also have a close button in that bubble which needs to use the same function that the timer uses. (want to avoid writing 2 exactly alike functions)
I'm hoping this there is an easy workaround for this like event:null or something.
The Timer Event:
private function overShareButton(event:MouseEvent):void
{
shareIconsTimer = new Timer(3000,1);
shareIconsTimer.addEventListener(TimerEvent.TIMER, fadeOutShareIcons);
shareIconsTimer.start();
fadeInShareIcons();
}
The close button event:
shareIcons.btn_closeIcons.addEventListener(MouseEvent.MOUSE_UP, fadeOutShareIcons);
The addThis Bubble Fade out function:
// Fade out the addThis Icons
private function fadeOutShareIcons(e:TimerEvent):void
{
shareIcons.buttonMode = false;
shareIcons.btn_closeIcons.buttonMode = false;
shareIcons.btn_email.buttonMode = false;
shareIcons.btn_facebook.buttonMode = false;
shareIcons.btn_myspace.buttonMode = false;
shareIcons.btn_digg.buttonMode = false;
shareIcons.btn_delicious.buttonMode = false;
shareIcons.btn_favorites.buttonMode = false;
shareIcons.btn_twitter.buttonMode = false;
shareIcons.btn_google.buttonMode = false;
shareIcons.btn_messenger.buttonMode = false;
shareIcons.btn_stumbleupon.buttonMode = false;
// STOP TIMER & Remove share icon button events...
shareIconsTimer.stop();
shareIconsTimer.removeEventListener(TimerEvent.TIMER, fadeOutShareIcons);
shareIcons.btn_email.removeEventListener (MouseEvent.MOUSE_UP, addThisToEmail);
shareIcons.btn_facebook.removeEventListener (MouseEvent.MOUSE_UP, addThisToFacebook);
shareIcons.btn_myspace.removeEventListener (MouseEvent.MOUSE_UP, addThisToMyspace);
shareIcons.btn_digg.removeEventListener (MouseEvent.MOUSE_UP, addThisToDigg);
shareIcons.btn_delicious.removeEventListener (MouseEvent.MOUSE_UP, addThisToDelicious);
shareIcons.btn_favorites.removeEventListener (MouseEvent.MOUSE_UP, addThisToFavorites);
shareIcons.btn_twitter.removeEventListener (MouseEvent.MOUSE_UP, addThisToTwitter);
shareIcons.btn_google.removeEventListener (MouseEvent.MOUSE_UP, addThisToGoogle);
shareIcons.btn_messenger.removeEventListener (MouseEvent.MOUSE_UP, addThisToLive);
shareIcons.btn_stumbleupon.removeEventListener (MouseEvent.MOUSE_UP, addThisToStumbleupon);
TweenLite.to(shareIcons, .2, {alpha:0});
}
My only choice right now is to create another function to handle the close button MouseEvent, any tips/thoughts appreciated! :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是您已将
fadeOutShareIcons
参数输入到TimerEvent
,因此它只能接受TimerEvent
。由于
MouseEvent
和TimerEvent
源自Event
,因此您可以使用它,以便两者可以一起工作。如果您还想在不带参数的情况下调用相同的函数,您可以传递设置为 null 的默认值。案例1
案例 2 使用默认参数,以便您可以调用
fadeOutShareIcons()
您还可以检查事件的来源,检查事件的类型:
Your problem here is you have typed your
fadeOutShareIcons
argument to aTimerEvent
, so it can only acceptTimerEvent
.Since
MouseEvent
andTimerEvent
descend fromEvent
you can use it so both can work together. If you want also call the same function without argument you can pass a default value setted to null.case 1
case 2 with default argument so you can call
fadeOutShareIcons()
You can also check from where event you came checking the type of the event:
将完成工作的代码放在一个单独的函数中(由两个事件处理程序调用)是否有效?无论如何,您的主要工作函数似乎都没有使用事件参数,因此该工作是否在另一个函数中完成也没关系。
Would putting the code that does the work in a separate function, which is called by both event handlers work? Your main work function doesn't appear to use the event parameter anyway, so it shouldn't matter if that work is done in another function.