简体   繁体   中英

unload content - main menu actionscript 3

i am new to flash actionscript 3 and trying to learn. I have a made a simple menu that has "start game" option. When the uses preses "start game", i hide the menu and the game starts. Now if i want to go "quit" and go the menu screen, how can i do this? How do i erase/stop the game that the user just played? thanks for any help on this matter ^^.

If you are coding on the timeline, the easiest way is to place your game (graphics, code, etc.) inside its own MovieClip, as opposed to on the main timeline. Then you can remove that wrapper MovieClip from the stage when the game is complete, either by jumping to a frame where that MovieClip doesn't exist, or by using removeChild in your code.

If you are using class file(s), the concept is the same: Put the game inside its own clip. In this case, your game would be wrapped in its own clip naturally because it would be a Sprite: class Game extends Sprite . But instead of using Game as the Document class, you want to create and add it to stage using code like the following:

var game:Game = new Game();
addChild(game);

Similarly, you can just remove your Game instance from the stage when the game is done using removeChild(game); .

This takes care of removing the game display when the game is over. The most important part , though, is that you must remove ENTER_FRAME , KEY_DOWN and any other listeners your game may have created. Even when you remove your game from the stage, these ENTER_FRAME handlers will continue to run (possibly indefinitely). This will often cause errors and performance problems because the game continues running even though you want it to stop. Therefore, when the game ends, first remove these event listeners using removeEventListener . (It's also helpful if you can limit your game to as few ENTER_FRAME listeners as possible, so that this step is easy.) Then it's safe to remove the game clip from the stage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM