Home > database >  why "else" statement doesn't work in AS3?
why "else" statement doesn't work in AS3?

Time:06-30

I create a button from movieClip if I click, the target will move to the next frame. if clicked again the target will return to the previous frame.

for the "IF" statement successfully executed, but for "else" the code doesn't work.

what's wrong with my code?

btn1.stop(); //stop the movieclip frame
btn1.addEventListener(MouseEvent.CLICK, clicked);
function clicked(e: MouseEvent): void {
    var namebtn:String = e.currentTarget.name;
    if (namebtn == "btn1"){
      e.currentTarget.nextFrame();
    } else {
      e.currentTarget.prevFrame();
    }
}

CodePudding user response:

You are using the moviclip instance name and that's why only if statement worked You can try this


btn1.stop(); //stop the movieclip frame
btn1.addEventListener(MouseEvent.CLICK, clicked);
function clicked(e: MouseEvent): void {
    if (btn1.currentFrame == 1){
      e.currentTarget.nextFrame();
    } elseif(btn1.currentFrame == 2) {
      e.currentTarget.prevFrame();
    }
}

  • Related