as3为每个项创建函数(在数组中?)

时间:2013-11-19 20:42:20

标签: arrays actionscript-3 for-loop drop-down-menu flash-cs6

我有一个名为 colorDDMenu 的动画片段,其中包含一个名为 color _ btn 的按钮和一个名为 menu _ mc 包含4个以上的按钮。

这个想法是用户点击颜色按钮,将出现包含4个按钮的动画片段,这样他们就可以点击4个按钮中的一个来选择颜色。除了颜色下拉菜单,我还有12个其他属性的下拉菜单(条纹,硬度,重量等)。

到目前为止,这是我的颜色下拉菜单代码:

    import flash.events.MouseEvent;

    menu_mc.visible = false;

    hover_btn.addEventListener(MouseEvent.CLICK, drop);

    function drop(event:MouseEvent)
    {
        menu_mc.visible = true;
        hover_btn.removeEventListener(MouseEvent.CLICK, drop);
        hover_btn.addEventListener(MouseEvent.CLICK, up);
    }

    function up(event:MouseEvent)
    {
        menu_mc.visible = false;
        hover_btn.removeEventListener(MouseEvent.CLICK, up)
        hover_btn.addEventListener(MouseEvent.CLICK, drop);
    }

它成功打开并关闭了我想要的菜单。

闭:

enter image description here

并打开:

enter image description here

现在到了这一步!

我正在尝试找到一种将此下拉方法应用于12个下拉菜单的有效方法。而不是为每个菜单键入此代码。

我想知道我是否可以通过使用Array和for循环的方法来实现这一点,这为每个菜单创建了这个函数。

2 个答案:

答案 0 :(得分:0)

您可以使用数组执行此操作。无需为每个菜单创建功能;只需将每个按钮链接到其菜单,然后单击即可使用该按钮。

您还可以创建一个类来实现相同的目标并保持清洁。

答案 1 :(得分:0)

我在博客上写了一篇简短的教程。关键是使用字典对象来跟踪单击按钮时应该发生的事情,以便将数据与按钮相关联。

以下是该教程的链接: http://plasticsturgeon.com/2010/06/my-favorite-menu-part-2-the-dictionary-object/

以下是该教程的代码:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.utils.Dictionary;

/**
 * ...
 * @author Zachary Foley
 */
public class SimpleXMLMenu extends Sprite
{
    private var loader:URLLoader;
    private var xmldata:XML;
    private var buttonData:Dictionary;
    private var _currentSelection:PrettyButton;
    private var _selectedData:String;

    public function SimpleXMLMenu()
    {
        // Create a loader to bring in the xml
        loader = new URLLoader();
        // create a dictionary to store the data associted with each pretty button.
        // don't bother those pretty buttons with the details.
        buttonData = new Dictionary();
        loadXML()
    }

    private function loadXML():void
    {
        // start the XML load.
        loader.load(new URLRequest("MenuData.xml"));
        loader.addEventListener(Event.COMPLETE, onXMLLoaded)
    }

    private function onXMLLoaded(e:Event):void
    {
        // get the data from the loader and cast it as XML
        xmldata = new XML(loader.data);
        // make a bunch of buttons from the xml
        makeButtons()
    }

    private function makeButtons():void
    {
        // The schema for the loop will change based on your xml structure.
        for (var i:int = 0; i < xmldata.menu.button.length(); i++)
        {
            // grab the xml for the button
            var xml:XML = xmldata.menu.button[i];
            // create a new button and set the label to the XML label.
            var b:PrettyButton = new PrettyButton(xml.label);
            // store the xml data in our dictionary to be looked up
            // when the button is clicked.
            buttonData[b] = xml;
            b.addEventListener(MouseEvent.CLICK, handleMenuClick)
            b.y = this.height;
            addChild(b);
            addChild(b);
        }
    }

    private function handleMenuClick(e:MouseEvent):void
    {
        if (_currentSelection != null) {
            _currentSelection.selected = false;
        }
        var buttonClicked:PrettyButton = e.target as PrettyButton;
        _currentSelection = buttonClicked;
        _currentSelection.selected = true;
        _selectedData = buttonData[e.target].content;
    }

    public function get selectedData():String { return _selectedData; }

}

}