Skip to main content
Known Participant
March 14, 2016
Question

AS3 Unload Problem

  • March 14, 2016
  • 2 replies
  • 445 views

I'm creating a game over screen and there is a retry button on it, when that button is clicked, the game over screen remove itself and jump to the main screen so player could restart it. When the retry button is clicked for the first time, it works fine, the game over screen is gone. But when it is clicked for the second time, the game over screen is here again, it just keeps coming back after the first retry. It seems the unload works for the first time only. Any help is greatly appreciated!


public class GameOverMenu extends BaseMenu {

  
public function GameOverMenu(stageRef: Stage = null) {
  
this.stageRef = stageRef;
  btnRetry
.addEventListener(MouseEvent.MOUSE_DOWN, returnMainMenu, false, 0, true);
  
}

  
private function returnMainMenu(e: MouseEvent😞 void {
  unload
(new MainMenu(stageRef));
  
}

}

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
March 15, 2016

this is really a mess and should be re-done.

but the best i can determine this might work:


public class GameOverMenu extends BaseMenu {

  
public function GameOverMenu(stageRef: Stage = null) {
  
this.stageRef = stageRef;
  btnRetry
.addEventListener(MouseEvent.MOUSE_DOWN, returnMainMenu, false, 0, true);
  
}

  
private function returnMainMenu(e: MouseEvent): void {
  unload
(new MainMenu(stageRef),this);
  
}

}

public class BaseMenu extends MovieClip {

public var stageRef: Stage;
public var loadNext: BaseMenu;

private ver unloadMenu:GameOverMenu;

public function BaseMenu() {
alpha = 0;
y = 1334;
}

public function unload(loadMe: BaseMenu = null,gameOverMenu:GamerOverMenu=null): void {
if (loadMe != null)
loadNext = loadMe;

if(gameOverMenu){

unloadMenu=gameOverMenu;

Tweener.addTween(unloadMenu, {
alpha: 0,
y: -1334,
time: 0.2,
onComplete: remove
});
}
}

public function remove(): void {
if (unloadMenu&unloadMenu.stage){
unloadMenu.parent.removeChild(unloadMenu);

unloadMenu=null

}

if (loadNext != null)
loadNext.load();
}

public function load(): void {

these 'this' references probably shoudl be loadNext

stageRef.addChild(this);
Tweener.addTween(this, {
alpha: 1,
y: 0,
time: 0.2
});
}

}
kglad
Community Expert
Community Expert
March 15, 2016

what's unload()?

hinc94Author
Known Participant
March 15, 2016

Yes

public class BaseMenu extends MovieClip {

public var stageRef: Stage;
public var loadNext: BaseMenu;

public function BaseMenu() {
alpha = 0;
y = 1334;
}

public function unload(loadMe: BaseMenu = null): void {
if (loadMe != null)
loadNext = loadMe;

Tweener.addTween(this, {
alpha: 0,
y: -1334,
time: 0.2,
onComplete: remove
});
}

public function remove(): void {
if (stageRef.contains(this))
stageRef.removeChild(this);

if (loadNext != null)
loadNext.load();
}

public function load(): void {
stageRef.addChild(this);
Tweener.addTween(this, {
alpha: 1,
y: 0,
time: 0.2
});
}

}
kglad
Community Expert
Community Expert
March 15, 2016

that's quite a mess.  no wonder you're having a problem.

use some trace statements to see which BaseMenu instance you're trying to remove and which unload method you're calling.