在多个对象AS3上侦听多个事件

时间:2010-09-20 12:52:59

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

我目前有8个不同对象的over,out和click事件。每个事件的over-out事件都是相同的(补间扩展对象,然后将其缩小为out状态)。

我之前曾要求一种简单的方法来声明这些事件,现在我正在寻找一种更简单的方法来处理它们。

这是我的代码:

//-----------GARAGE
function growGarage(e:MouseEvent):void{
     scaleTweenX = new Tween(map_garage, "scaleX", Elastic.easeOut, 0.648, 1, 0.5, true);
     scaleTweenY = new Tween(map_garage, "scaleY", Elastic.easeOut, 0.648, 1, 0.5, true);
}
function shrinkGarage(e:MouseEvent):void{
     scaleTweenX = new Tween(map_garage, "scaleX", Elastic.easeOut, 1, 0.648, 0.5, true);
     scaleTweenY = new Tween(map_garage, "scaleY", Elastic.easeOut, 1, 0.648, 0.5, true);
}
function garageClick(e:MouseEvent):void{
     trace("clicked");
}
//-----------B&B
function growBandb(e:MouseEvent):void{
     scaleTweenX = new Tween(map_bandb, "scaleX", Elastic.easeOut, 0.648, 1, 0.5, true);
     scaleTweenY = new Tween(map_bandb, "scaleY", Elastic.easeOut, 0.648, 1, 0.5, true);
}
function shrinkBandb(e:MouseEvent):void{
     scaleTweenX = new Tween(map_bandb, "scaleX", Elastic.easeOut, 1, 0.648, 0.5, true);
     scaleTweenY = new Tween(map_bandb, "scaleY", Elastic.easeOut, 1, 0.648, 0.5, true);
}
function bandbClick(e:MouseEvent):void{
     trace("clicked");
}
//-----------FACTORY
function growFactory(e:MouseEvent):void{
     scaleTweenX = new Tween(map_factory, "scaleX", Elastic.easeOut, 0.648, 1, 0.5, true);
     scaleTweenY = new Tween(map_factory, "scaleY", Elastic.easeOut, 0.648, 1, 0.5, true);
}
function shrinkFactory(e:MouseEvent):void{
     scaleTweenX = new Tween(map_factory, "scaleX", Elastic.easeOut, 1, 0.648, 0.5, true);
     scaleTweenY = new Tween(map_factory, "scaleY", Elastic.easeOut, 1, 0.648, 0.5, true);
}
function factoryClick(e:MouseEvent):void{
     trace("clicked");
}
//-----------SCHOOL
function growSchool(e:MouseEvent):void{
     scaleTweenX = new Tween(map_school, "scaleX", Elastic.easeOut, 0.648, 1, 0.5, true);
     scaleTweenY = new Tween(map_school, "scaleY", Elastic.easeOut, 0.648, 1, 0.5, true);
}
function shrinkSchool(e:MouseEvent):void{
     scaleTweenX = new Tween(map_school, "scaleX", Elastic.easeOut, 1, 0.648, 0.5, true);
     scaleTweenY = new Tween(map_school, "scaleY", Elastic.easeOut, 1, 0.648, 0.5, true);
}
function schoolClick(e:MouseEvent):void{
     trace("clicked");
}

我尝试使用单个函数,然后使用“this”作为补间的对象,但扩展了整个阶段。

有没有办法压缩这段代码?

干杯,

1 个答案:

答案 0 :(得分:2)

您可以使用this而不是e.currentTarget使用单个处理程序。 (当e是你的传入事件时)