使用MovieClip目标名称将变量传递给函数

时间:2012-02-22 17:44:54

标签: flash actionscript-3 function mouseevent event-listener

如何组合以下功能并仍将不同的字符串传递给buildUI();函数?

我有两个函数只在最后调用一个函数并将一个String值传递给函数。这个字符串值是唯一不同的东西。

以下是我的eventlisteners和函数,现在是:

    female_start.addEventListener(MouseEvent.MOUSE_DOWN, startFemale);
    male_start.addEventListener(MouseEvent.MOUSE_DOWN, startMale);


//FUNCTIONS THAT DO THE SAME THING AND BOTH CALL  buildUI BUT PASS A DIFFERENT STRING. 
function startMale(event:MouseEvent):void 
        {

            female_start.removeEventListener(MouseEvent.MOUSE_DOWN, startFemale);
            male_start.removeEventListener(MouseEvent.MOUSE_DOWN, startMale);
            var removeMale = new Tween(male_start, "x", Strong.easeInOut, 540,1080, 2, true); 
            var removeFemale = new Tween(female_start, "x", Strong.easeInOut, 0,-540, 2, true); 
//THIS IS THE ONLY DIFFERENCE BETWEEN THE TWO FUNCTIONS
            buildUI("Male");
     }


function startFemale(event:MouseEvent):void
            {

            female_start.removeEventListener(MouseEvent.MOUSE_DOWN, startFemale);
            male_start.removeEventListener(MouseEvent.MOUSE_DOWN, startMale);
            var removeMale = new Tween(male_start, "x", Strong.easeInOut, 540,1080, 2, true); 
            var removeFemale = new Tween(female_start, "x", Strong.easeInOut, 0,-540, 2, true); 
//THIS IS THE ONLY DIFFERENCE BETWEEN THE TWO FUNCTIONS
            buildUI("Female");
     }

1 个答案:

答案 0 :(得分:1)

female_start.addEventListener(MouseEvent.MOUSE_DOWN, function(e){start(e, "female");});
male_start.addEventListener(MouseEvent.MOUSE_DOWN, function(e){start(e, "male");});
function start(event:MouseEvent, sex:String):void{
  //do all the stuff
  buidUI(sex);
}