Simple Platform Game Basics Part 4c
The Main class for our horse and hurdles game.
This class contains a ScreenOrganizer.
The screen organizer holds a reference to each class that will make up the game.
When the changeScreen or switchScreen methods are use a black cover fades over the current screen,
then fades out revealing the next screen chosen.
There are also various events and properties that help us use the ScreenOrganizer in the way we want.
The main event that happens is the ScreenChangeEvent.SCREEN_CHANGE after a screen change is complete.
There are also the ScreenChange.COVER and UNCOVER events.
COVER happens just after the screen has been changed but before the cover has been removed.
UNCOVER happens just before SCREEN_CHANGE, right after the cover is faded out.
Keeping a public static reference to the ScreenOrganizer gives each class of the game
a quick way to switch to other screens and/or reference the other classes of the game.
HurdlesMain.as
package Tutorials
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import com.actiontad.gameUtils.ScreenOrganizer;
import com.actiontad.basicGameEvents.ScreenChangeEvent;
/**
* Simple Platform Game Basics Part 4c The HurdlesMain Class and ScreenOrganizer Class
*
* This example shows how to use a ScreenOrganizer for basic organization of the classes that make up a game.
*
* In this example our game has 2 main classes, HurdlesTitleScreen and Hurdles.
*
* Part 4a went over the details of the Hurdles code.
* Part 4b went over the title screen code.
*
* This part uses those 2 classes as the screenClasses for the ScreenOrganizer that this class has.
*
* It brings those 2 classes together in a common way.
*
*
* @author (t)ad
*/
[SWF(frameRate = '25', backgroundColor = '0xffffff', width = '650', height = '350')]
public class HurdlesMain extends Sprite
{
public static var theScreenOrg:ScreenOrganizer;
public function HurdlesMain() {
if (stage) ini(null) else addEventListener(Event.ADDED_TO_STAGE, ini);
}
private function ini(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, ini);
//No import is needed if the classes are in the same package as this class.
//The classes will be instantiated at this time, but not added to the display until swicthScreen or changeScreen is called.
theScreenOrg = new ScreenOrganizer(this, [HurdlesTitleScreen, Hurdles]);
theScreenOrg.coverAnimationRate = 66.6;
//A quick way to reference things within one of the classes in the resulting screens Array:
theScreenOrg.screens[0].startButtonSprite.addEventListener(MouseEvent.CLICK, startGame);
theScreenOrg.switchScreen(0);//switch to the title screen.
}
/**
* The ScreenChangeEvent.SCREEN_CHANGE event will happen after the screen has finished changing.
* There are other events along the way, namely COVER, and UNCOVER,
* and you can instruct the ScreenOrganizer to change and stay covered until you dispatch a UNDER_COVER_CHANGES_COMPLETE ScreenChangeEvent.
* You do so by setting waitForUnderCoverChanges to true.
*
* In this case nothing is added to the engine for display until beginGame is called.
* The ScreenChangeEvent.SCREEN_CHANGE happens after the screen has finished changing, meaning after everything is complete.
*
* What we want is to change to the Hurdles class AND call beginGame while the cover is still over everything.
* That way, when the cover is gone we don't still see the title screen for a split second, even though we have not removed it,
* just put the game on top.
*
* So, to do that we listen for the COVER event, which happens after the next screen has been placed, but before the cover is removed.
*/
private function startGame(e:MouseEvent):void {
theScreenOrg.addEventListener(ScreenChangeEvent.COVER, callBeginGame);//listener first
theScreenOrg.changeScreen(1);//now change the screen to the Hurdles Engine, when changed, but still covered, the above handler will fire.
}
private function callBeginGame(e:ScreenChangeEvent):void {
theScreenOrg.removeEventListener(ScreenChangeEvent.COVER, callBeginGame);
theScreenOrg.screens[1].beginGame();
}
}
}
The Result
See the example in its own window
here.
That's all for now!
Thanks for reading!
The next platformer tutorial will use the basicEngine and go over some A.I. techniques for enemies.
You can find the source code for the actiontad.basicGameObjects and other packages
here.