The State class represents your game state. You will extend this class to be able to add objects to your game.
In your own GameState class which extends State, you will have to add your objects after the initialiaze function called by the CitrusEngine class after the state is added to the stage.
import citrus.core.State; public class GameState extends State { public function GameState() { super(); } override public function initialize():void { // Don't forget to call it, it initializes the view and get the input. super.initialize(); // You can start to create your objects there. } }
If you use a physics engine like Box2D or Nape, it will often be the first object you wish to add. To add an object to the State, you will use the add method. Examples:
var physics:Box2D = new Box2D("box2D"); physics.visible = true; add(physics); // you can also add directly an object to the State without using a variable. add(new Platform("ground", {x:stage.stageWidth / 2, y:stage.stageHeight, width:stage.stageWidth})); // or register in one line: var hero:Hero = add(new Hero("hero", {x:300}) as Hero);
To remove an object on the State you will use the remove function or set its kill property to true. It will automatically be destroyed and removed.
remove(hero); hero.kill = true; // same effect.
You can also access directly to some objects thanks to their names or their classes. It's often useful when you use a level editor and the ObjectMakers, example:
ObjectsMaker.FromMovieClip(levelSWF); var hero:Hero = getObjectByName("hero") as Hero; var enemies:Vector<CitrusObject> = getObjectsByType(Enemy);
Note it's also possible to add/remove objects to your State from everywhere in your code:
CitrusEngine.getInstance().state.add(new Coin("coin", {x:Math.random * 500, y:Math.random() * 500}));
If you use Starling or Away3D as your view renderer, you will have to use respectively StarlingState and Away3DState. They are similar to the original State class (there isn't multiple inheritance in AS3, so in fact all the states class are just a bridge to a MediatorState) the only difference is they inherit from Starling's Sprite and Away3D's ObjectContainer3D which are both display object container. All the methods are similar, and they're used the same way.
At anytime in your game you can change your State to a new one or relaunch the current one. The current state will automatically destroyed and removed its object, and will be replaced by the new one.
//restart the State: CitrusEngine.getInstance().state = this; // load an other level: CitrusEngine.getInstance().state = new GameState2();
You can also use the LevelManager class to manage your level.
It's very easy to create a User Interface on top of your game state. The State extends a display object container, its nature depends if you use the display list (Flash Sprite), Starling (Starling Sprite) or Away3D (ObjectContainer3D).
If we have created a Hud, we will add it this way:
var hud:Hud = new Hud(); addChild(hud);
It will automatically be on top of your game objects since they are added at the index 0. You can modify this index to fit your needs.
By default the State class will create its own view:
The Citrus Engine is extendable, so it's also possible to create your own view. This is how the BlittingView is set up. Override the createView function this way:
override protected function createView():CitrusView { return new BlittingView(this); }
Now you're running your view into a Blitting mode.