我正在开发一款应用程序来向儿童学习分数。
我有几个馅饼。所有的馅饼都是第1帧中时间轴上的MovieClip位置,并且位于屏幕上的同一位置。根据数字(2到12),可以看到一个饼图,用户可以根据问题点击这些部分隐藏多个部分。
例如:显示2/9件。因此,用户必须隐藏7件。就像一个有2,3,4,5件等的馅饼。每次代码看起来几乎相同。我想知道如何用更少的代码让这个工作。
如何将此代码转换为更少的行?
if(number == 9) {
//pie 9
pie9_mc.visible = true;
pie9_mc.p9_p1_mc.alpha = 1;
pie9_mc.p9_p2_mc.alpha = 1;
pie9_mc.p9_p3_mc.alpha = 1;
pie9_mc.p9_p4_mc.alpha = 1;
pie9_mc.p9_p5_mc.alpha = 1;
pie9_mc.p9_p6_mc.alpha = 1;
pie9_mc.p9_p7_mc.alpha = 1;
pie9_mc.p9_p8_mc.alpha = 1;
pie9_mc.p9_p9_mc.alpha = 1;
pie_mc = pie9_mc;
pie9_mc.p9_p1_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p2_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p3_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p4_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p5_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p6_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p7_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p8_mc.addEventListener(MouseEvent.CLICK, hidePiece);
pie9_mc.p9_p9_mc.addEventListener(MouseEvent.CLICK, hidePiece);
}
function hidePiece(e: MouseEvent): void{
piece_mc = MovieClip(e.currentTarget);
var tweenFadeOut:Tween = new Tween(piece_mc, "alpha", None.easeOut, 1, 0.5, 2, true);
}
答案 0 :(得分:1)
pie9_mc["p9_p1_mc"].alpha = 1;
与
相同pie9_mc.p9_p1_mc.alpha = 1;
因此,您的代码转换较少的行,如下面的代码。
if(number == 9) {
//pie 9
pie9_mc.visible = true;
for (var i:int=1; i<=number; i++){
var foo: String = "p"+number.toString()+"_p"+i.toString()+"_mc";
pie9_mc[foo].alpha = 1;
pie9_mc[foo].addEventListener(MouseEvent.CLICK, hidePiece);
}
pie_mc = pie9_mc;
}
function hidePiece(e: MouseEvent): void{
piece_mc = MovieClip(e.currentTarget);
var tweenFadeOut:Tween = new Tween(piece_mc, "alpha", None.easeOut, 1, 0.5, 2, true);
}