创建调用相同函数并传递变量的事件侦听器

时间:2012-02-20 21:05:03

标签: actionscript-3 function addeventlistener

我完全搞砸了我的代码,并且在将其复制到StackOverflow时没有更改Movieclip名称,所以没有任何意义。

我想知道是否有更好的方法来完成以下as3:

//THREE EVENT LISTENERS

                shoe_icon.addEventListener(MouseEvent.MOUSE_DOWN, shoeApp);
                top_icon.addEventListener(MouseEvent.MOUSE_DOWN, topApp);
                bottom_icon.addEventListener(MouseEvent.MOUSE_DOWN, bottomApp);

//THREE FUNCTIONS THAT ALL CALL THE SAME FUNCTION BUT PASS A DIFFERENT VALUE
        function shoeApp(event:MouseEvent):void{
            buildSlider("Shoe");
        }
        function topApp(event:MouseEvent):void{
            buildSlider("Top");
        }
        function bottomApp(event:MouseEvent):void{
            buildSlider("Bottom");
        }
//THIS IS MY MAIN FUNCTION WHICH IS ALL BASED ON THE SLIDERTYPE VARIABLE IT RECEIVES 
        function buildSlider(sliderType:String):void{

        }

4 个答案:

答案 0 :(得分:2)

不确定你想在这做什么。因为听众所有的听众都被添加到MOUSE_DOWN上的舞台哇,你无论如何都会调用所有3个函数。此时,您可以通过单个函数来处理这个问题,该函数将使用不同的参数调用buildSlider 3次。

也许你想要处理这取决于你的MOUSE_DOWN?在这种情况下,您应该将侦听器添加到不同的对象而不是舞台。

答案 1 :(得分:0)

你有什么理由不能这样做吗?

    stage.addEventListener(MouseEvent.MOUSE_DOWN, addApps);

    function addApps(event:MouseEvent):void{
        buildSlider("Shoe");
        buildSlider("Top");
        buildSlider("Bottom");
    }

//THIS IS MY MAIN FUNCTION WHICH IS ALL BASED ON THE SLIDERTYPE VARIABLE IT RECEIVES 
function buildSlider(sliderType:String):void{

}

答案 2 :(得分:0)

在我看来,您正在尝试在MovieClip和String值之间创建关系; Dictionary可以帮到你。

private var buttonClipsToActionsMap: Dictionary = new Dictionary();

public function init() : void {
    buttonClipsToActionsMap[buttonShoe] = "Shoe";
    buttonClipsToActionsMap[topApp] = "Top";

    // Bind the Event Listener to the Stage.
    stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownEvent);
}

private function onMouseDownEvent(event : MouseEvent) : void {
    // Check to see if the MovieClip clicked is assosated with an Action.
    var action : String = buttonClipsToActionsMap[event.currentTarget];

    // If it does then invoke buildSlider().
    if (action != null) {
        buildSlider(action);
    }
}

请注意,由于Event Bubbling,您只需要在舞台上添加一个事件侦听器,而不是每个MovieClip上有3个单独的事件侦听器。

我们将词典用作Map, or HashTable;一个DataStructure,它允许两个值之间的简单1-1关系。

答案 3 :(得分:-1)

你应该拥有的是

//THREE EVENT LISTENERS

buttonShoe.addEventListener(MouseEvent.MOUSE_UP, handleButtonUp);
buttonTop.addEventListener(MouseEvent.MOUSE_UP, handleButtonUp);
buttonBottom.addEventListener(MouseEvent.MOUSE_UP, handleButtonUp);

//THREE FUNCTIONS THAT ALL CALL THE SAME FUNCTION BUT PASS A DIFFERENT VALUE
function handleButtonUp(event:MouseEvent):void
{
    var argument:String = "";

    switch(event.target)
    {
        case buttonShoe:
        {
            argument = "Shoe";
            break;
        }
        case buttonTop:
        {
            argument = "Top";
            break;
        }
        case buttonBottom:
        {
            argument = "Bottom";
            break;
        }
        default:
        {
            trace("something bad happened, we should handle this somehow. but later");
            break;
        }
    }
    buildSlider(argument);       
}

//THIS IS MY MAIN FUNCTION WHICH IS ALL BASED ON THE SLIDERTYPE VARIABLE IT RECEIVES 
function buildSlider(sliderType:String):void
{
   // your code
}