动作脚本3 - 多个电影按钮剪辑?

时间:2011-05-24 18:54:50

标签: flash actionscript-3 action flash-cs5

所以我有一个代码,如下所示:

import flash.events.MouseEvent; 

instance160.stop();

function playMovie(event:MouseEvent):void 

{ 
instance160.play();
} 
button160.addEventListener(MouseEvent.CLICK, playMovie);

我想添加多个按钮,而不仅仅是这个按钮。我通过反复试验得知我不能只是连续列出实例,我还发现我不能只复制代码并更改实例名称。我只需要知道如何使用多个按钮/剪辑。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

“Event”类(MouseEvent扩展)具有一个名为“currentTarget”的属性,该属性将引用传递给调度该事件的任何对象。所以你可以这样做:

myClip.addEventListener(MouseEvent.CLICK, playMovie, false, 0, true);
myClip2.addEVentListener(MouseEvent.CLICK, playMovie, false, 0, true);
...
myClip9999.addEventListener(MouseEvent.CLICK, playMovie, false, 0, true);

function playMovie(event:MouseEvent):void {
 event.currentTarget.play();
}

这或多或少是你想要的吗?

答案 1 :(得分:0)

我使用了这个我曾经做过的小班 - 这使得添加监听器的乘法按钮变得更快,更容易和更清洁:

package
{
    public class ButtonCollection extends Object
    {
        // current list
        private var _clist:Array = [];

        /**
         * Merge instances of SimpleButton with listeners
         * @param list A list of buttons and listeners to merge
         */
        public function merge(...list):void
        {
            var i:Object;
            for each(i in list)
            {
                i.button.addEventListener(i.event, i.callback);
                _clist[_clist.length] = i;
            }
        }

        /**
         * Removes all previously added listeners
         */
        public function clear():void
        {
            var i:Object;
            for each(i in _clist)
            {
                i.button.removeEventListener(i.event, i.callback);
            }

            _clist = [];
        }
    }
}

使用:

这就是我使用这个类将一些监听器添加到我用实例名称 a b 创建的两个按钮的方法。

var bc:ButtonCollection = new ButtonCollection();

bc.merge(
    {button: a, event: MouseEvent.CLICK, callback: _click},
    {button: b, event: MouseEvent.CLICK, callback: _click},
    {button: b, event: MouseEvent.MOUSE_OVER, callback: _mouseOver}
);

function _click(e:MouseEvent):void
{
    trace(e.target.name + " was clicked");
}

function _mouseOver(e:MouseEvent):void
{
    trace(e.target.name + " was rolled over");
}

该类还为您提供了一个简单的选项,可以在完成后删除所有以前添加的侦听器:

bc.clear();