Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you should remove one <code>parent</code> to make sure you are actually removing only the game:</p> <pre><code>function BackToMenu(i:MouseEvent):void { var BaseMovie:MovieClip = parent as MovieClip; BaseMovie.parent.removeChild(BaseMovie); } </code></pre> <p>This should take care of your most pressing problem, and allow you to return to the menu. You have, however, not really unloaded the game, but only removed it from the display list. This often means, that there are still sounds running, active key and/or mouse listeners, etc. - these must all be taken care of! </p> <p>And, like I said, this will only fix your immediate problem. It is, however, neither a permanent solution, nor a good one: Since the main SWF is responsible for loading the games, it should also be responsible for disposing of them. You <em>should</em> put cleanup code into your game, but it should only be concerned with stopping any running scripts, sounds, etc. - simple rule: anything that is <em>started</em> within the game, should be <em>stopped</em> within the game. But it should not try to access objects further up in the display hierarchy, or try to unload itself. </p> <p>The much better way to do this is by replacing all the above code, and letting the main SWF take care of removing the game, as well as unloading it from memory. For this, you have to do three things:</p> <ol> <li><p>Instead of writing actual <code>removeChild</code> calls, etc., let your button dispatch a custom event to notify the main SWF that it should now be removed:</p> <pre><code>function onBackButtonClicked( event:MouseEvent ):void { destroyGame(); // this would be the function that stops all the scripts dispatchEvent( new Event( "FINISH_GAME", true ) ); } </code></pre> <p>Note that "FINISH_GAME" is now a "bubbling" event, i.e. it travels downward in the display hierarchy. We can now listen for this event in any ancestor display object containing the game.</p></li> <li><p>In your main SWF, add an event listener to the <code>Loader</code> when the game was successfully loaded. This is done in the event listener that is called when the load process completes:</p> <pre><code>function onLoadComplete( event:Event ):void { var loader:Loader = event.target.loader; loader.addEventListener( "FINISH_GAME", onFinishGame, true ); } </code></pre></li> <li><p>Use the corresponding event handler to remove the game clip:</p> <pre><code>function onFinishGame( event:Event ):void { var loader:loader = event.currentTarget; loader.parent.removeChild( loader ); loader.unloadAndStop(); } </code></pre></li> </ol> <p>A few more things to consider:</p> <ul> <li><p>The naming conventions in ActionScript advise us to use lower case names for methods and variables, and upper case only for types.</p></li> <li><p>The same naming conventions suggest we use either "on" or "handle" as a prefix for event listeners, along with the name of the event. Thus, it should be <code>onBackToMenu</code> or rather, <code>onBackButtonClicked</code>, etc.</p></li> <li><p>Since I don't know anything about the code you use for loading, I just assumed you have a complete listener, and you don't keep references to the loader. If you use a member variable, you can use that instead of <code>event.target</code>, resp. <code>event.currentTarget</code>.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload