我有一个简单的菜单,在每个项目悬停时,播放一个影片剪辑,然后在mouse_out上反向播放影片剪辑。我要做的是在点击时显示第三个状态(活动)。我想我需要做一些事情:
单击时,gotoAndStop(5)//五是我的活动框架的位置 同时删除触发函数的事件侦听器以反向播放影片。 然后,当单击另一个菜单项时,将事件监听器重新添加到上一个菜单项,这样它就不会卡在“活动”状态
我无法弄清楚如何做到这一点。我的代码如下:
// IMPORTS
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.external.ExternalInterface;
// EVENT LISTENERS
//arrow
mcArrow.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcArrow.addEventListener(MouseEvent.MOUSE_OUT,mout);
//dots
mcDots.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcDots.addEventListener(MouseEvent.MOUSE_OUT,mout);
//music
mcMusic.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcMusic.addEventListener(MouseEvent.MOUSE_OUT,mout);
//home
mcHome.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcHome.addEventListener(MouseEvent.MOUSE_OUT,mout);
//padlock
mcPadlock.addEventListener(MouseEvent.MOUSE_OVER,mover);
mcPadlock.addEventListener(MouseEvent.MOUSE_OUT,mout);
// FUNCTIONS
function mover(e:MouseEvent):void {
stopPlayReverse(e.currentTarget as MovieClip);
e.currentTarget.play();
//var fadeIn:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 0, 1, 0.5, true);
}
function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
//var fadeOut:Tween = new Tween(mcToolTip, "alpha", Strong.easeOut, 1, 0, 0.5, true);
}
function playReverse(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
} else {
mc.prevFrame();
}
}
function stopPlayReverse(mc:MovieClip):void {
if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
mc.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
答案 0 :(得分:0)
首先,我将创建一个MenuItem类,用于设置菜单项的事件侦听器和方法。在MenuItem类中,创建一个Boolean类型的'isActive'属性,用于跟踪菜单项的当前状态。这是该课程的快速模型:
package {
import flash.display.MovieClip;
import flash.events.*;
public class MenuItem extends MovieClip {
public var isOver:Boolean = false;
public var isActive:Boolean = false;
public static var CLICK:String = 'menu_item_click';
public function MenuItem() {
addEventListener(MouseEvent.MOUSE_OVER, mover);
addEventListener(MouseEvent.MOUSE_OUT, mout);
addEventListener(MouseEvent.CLICK, mclick);
addEventListener(Event.ENTER_FRAME, onFrame);
}
private function mover(e:MouseEvent):void {
isOver = true;
}
private function mout(e:MouseEvent):void {
isOver = false;
}
private function mclick(e:MouseEvent):void {
isActive = true;
goToAndStop(5); // go to active frame
}
private function onFrame(e:Event):void {
if (isActive) return; // don't do anything if this menu item is active
if (isOver) { // if the mouse is over the menu item
if (currentFrame >= 4) { // make sure we don't go to frame 5, the active frame
nextFrame();
}
} else {
prevFrame(); // or play in reverse. If at frame 1, prevFrame() won't do anything
}
}
}
}
ENTER_FRAME侦听器将连续运行并根据isActive和isOver状态执行操作。这消除了删除侦听器和为playReverse和stopPlayReverse创建其他方法的需要。
其次,我将所有菜单项放在名为MenuContainer的容器类中。最简单的方法是在Flash中创建一个空的影片剪辑,然后将所有菜单项拖入其中。使用MenuContainer的Class值导出Actionscript的容器。这是MenuContainer类的模型:
package {
import flash.display.MovieClip;
import flash.events.*;
public class MenuContainer extends MovieClip {
public function MenuContainer() {
addEventListener(MenuItem.CLICK, onMenuItemClick);
}
private function onMenuItemClick(e:MouseEvent):void {
var clickedMenuItem:MenuItem = MenuItem(e.target); // the clicked menu item
for (var i:int = 0; i < this.numChildren; i++) { // loop through the menu items
var menuItem:MenuItem = MenuItem(this.getChildAt(i)); // get the loop menu item
if (menuItem != clickedMenuItem) { // if the loop menu item is not the clicked menu item
menuItem.isActive = false; // then isActive is false
}
}
}
}
}
答案 1 :(得分:0)
根据你的代码,另一个选项是有一个辅助变量来指示哪个菜单项当前处于活动状态,所以我们可以在点击另一个项目后停用它...这意味着每个菜单项都会执行此项点击功能:
private var activeMenuItem:MenuItem; //your auxiliary variable
private function onClick(e:MouseEvent):void {
if(activeMenuItem) {
//if there is an active menu item re-enable it
activeMenuItem.gotoAndStop(1);
activeMenuItem.mouseEnabled=true;
}
//set the active menu item to the clicked one
activeMenuItem=e.currentTarget;
//and set its state to active and mouse disabled
activeMenuItem.gotoAndStop(5);
activeMenuItem.mouseEnabled=false;
}