In this tutorial we add a game over and main menu screen to the game.
Although it might not be immediately obvious, state management, like switching from a menu screen to a game level and back again, is not provided by the PushButton XML level loading classes. While you can load a level, it will in fact load all the entities in addition to those that are currently loaded. Clearing currently loaded entities is not a function of the level loading, and so before a new level is loaded you need to manually destroy the existing entities for yourself.
The easiest way to do this is to create a component that listens for a global event to be trigger and destroy the entity to which it belongs in response. The DestroyOnLevelClearComponent does just that. It listens for the level clear event dispatched by the main stage object (a convenient and central object that all components can listen to).
DestroyOnLevelClearComponent.as
public class DestroyOnLevelClearComponent extends EntityComponent
{
public static const LEVEL_CLEAR:String = "LevelClear";
public function DestroyOnLevelClearComponent()
{
super();
}
protected override function onAdd():void
{
super.onAdd();
Global.mainStage.addEventListener(LEVEL_CLEAR, levelClear);
}
protected override function onRemove():void
{
super.onRemove();
Global.mainStage.removeEventListener(LEVEL_CLEAR, levelClear);
}
protected function levelClear(event:Event):void
{
owner.destroy();
}
}
Now all entities created in the course of the level, either loaded directly from an XML declaration or created at runtime, get the new DestroyOnLevelClearComponent component.
The SwitchLevelComponent is used to dispatch the level clear event, and then request that a new level be loaded by the LevelManager. We have used the keyboard events, as opposed to checking to see if a key has been pressed every frame, to ensure that the level clearing and loading process is done once.
SwitchLevelComponent.as
public class SwitchLevelComponent extends EntityComponent
{
public var level:int = -1;
public function SwitchLevelComponent()
{
super();
}
protected override function onAdd():void
{
super.onAdd();
Global.mainStage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}
protected override function onRemove():void
{
super.onRemove();
Global.mainStage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
}
protected function keyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
Global.mainStage.dispatchEvent(new Event(DestroyOnLevelClearComponent.LEVEL_CLEAR));
LevelManager.instance.loadLevel(level);
}
}
}
Two new templates are created that display an image on the screen. The first displays a “game over” message, while the second display a “start game” message. Both use the SwitchLevelComponent to switch between the main menu level (level 0) and the game level (level 1).
The player entity gets a new DeathHandlerComponent that will display the game over message when it dies.
The EnemyControllerComponent will also create the game over entity when the bottom enemy reaches the bottom of the screen.
EnemyControllerComponent.as
public override function onTick(tickRate:Number):void
{
// ...
if (this === bottomEnemy &&
position.y > screenHeight - screenBuffer &&
!gameOverTriggered)
{
TemplateManager.instance.instantiateEntity("GameOver");
gameOverTriggered = true;
}
}
The new main menu level is created in a file called level0.xml. This follows the same format as the level1.xml file, and it creates a single main menu entity that can be used to reload the game level (level 1).
In C++ an array is a set of consecutive objects of the same type, in memory. We see how to crea...
A database is a set of related tables. This is part 1, division 1 of a series I have on databas...
To easy way lean java programming language with a basic concepts which will help to build basic...
its an tutorial to make a virus /worm in C language...
cracking unix system password all about unix cracking it quite easily...
In this tutorial we add mouse interactivity to the scene....
In this tutorial we allow the view of the isometric scene to be moved with the mouse....
In this tutorial we show the height of an isometric object by adding a shadow....
In this tutorial we modify the appearance of the isometric cube at runtime....
In this tutorial we add some animated isometric boxes to the scene....