Henrique
Henrique

Reputation: 145

Actionscript 3.0 coding less lines

I am working on a app to learn fractions to children.

I have several pies with pieces. All of the pies are MovieClips places on the timeline in frame 1 and on the same place on the screen. Depending on the number (2 till 12) a pie will be visible and a user is able to click on the pieces to hide several pieces according to the question.

For example: Show 2/9 pieces. So 7 pieces must be hide by the user. Like a pie with 2, 3 , 4, 5 pieces and so on. The code looks almost the same each time. I am wondering how i can let this work with less code.

How to convert this code to less lines?

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);
}

Upvotes: 0

Views: 40

Answers (1)

Yasuyuki  Uno
Yasuyuki Uno

Reputation: 2547

pie9_mc["p9_p1_mc"].alpha = 1;

works same as

pie9_mc.p9_p1_mc.alpha = 1;

Hence your code convert less lines like following code.

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);
}

Upvotes: 1

Related Questions