Replace a MovieClip with another MovieClip, keeping the same instance name

advertisements

I'm new to actionscript so this question might be a stupid one.

I'm trying to replace a movieclip with another movieclip, while keeping the instance name of the previous.

I have a menu with a selection of buttons, each leading to the same screen with a movieclip and a scrubber bar. I tried defining the movieclip through a variable, then tried redefining it through an event listener function, but I'm guessing I can't do like this:

var MC: movieclipsymbol1 = new movieclipsymbol1;

private function selectionscreen(): void {

  selectionscreenbutton1.addEventListener(MouseEvent.CLICK, screenbutton1);
  selectionscreenbutton2.addEventListener(MouseEvent.CLICK, screenbutton2);

  private function screenbutton1(event: MouseEvent): void {
    var MC: movieclipsymbol1 = new movieclipsymbol1;
    movieclipscreen();
  }

  private function screenbutton2(event: MouseEvent): void {
    var MC: movieclipsymbol2 = new movieclipsymbol2;
    movieclipscreen();
  }
}

public function movieclipscreen(): void {
  stage.addChild(MC);
}

Because of the scrubber bar code I did, I need to keep the instance for the movieclips the same. Is the approach I'm using completely off?


You have to remove var MC from both handlers, as you want your new MC to be accessible from outside of the handlers. But also you need to change the type of class variable MC so that it could hold either movieclipsymbol1 or movieclipsymbol2. The most common choice for the type in there is MovieClip. So, you have to change your functions like this:

var MC:MovieClip = new movieclipsymbol1();
private function screenbutton1(event: MouseEvent): void {
    clearOldMC();
    MC = new movieclipsymbol1();
    movieclipscreen();
}
private function screenbutton2(event: MouseEvent): void {
    clearOldMC();
    MC = new movieclipsymbol2();
    movieclipscreen();
}
private function clearOldMC():void {
    if (MC.parent) MC.parent.removeChild(MC);
}

The new function removes the previously displayed movie clip, regardless of its type.