Talking about Platform game game, Actionscript 3, Flash and Game development.
Every game should start at least with a simple title screen, and that’s what we are going to do with our FlashPunk project.
This is what you’ll get at the end of this step:
Press X to start playing then move the player as see in the second step.
Let’s see the logic behind it. Now we have the main game and the title screen.
Although managing the title screen as an Entity of the main world, I prefer to keep the title screen and the game itself in two different worlds.
This is what flashpunk
class becomes
package {
import net.flashpunk.Engine;
import net.flashpunk.FP;
[SWF(width="640",height="480",frameRate="60")]
public class flashpunk extends Engine {
public function flashpunk() {
super(640,480,60,false);
}
override public function init():void {
FP.screen.color = 0x332222;
FP.world=new mainMenu();
}
}
}
Unlike in the second step, I am giving a different background color (line 10) and I am assigning mainMenu as world (line 11).
So everything lies in the mainMenu
class:
package {
import net.flashpunk.Entity;
import net.flashpunk.World;
import net.flashpunk.FP;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import net.flashpunk.graphics.Text;
public class mainMenu extends World {
public function mainMenu() {
var titleText:Text = new Text("Press X to Start");
var textEntity:Entity = new Entity(0,0,titleText);
textEntity.x = (FP.width/2)-(titleText.width/2);
textEntity.y = (FP.height/2)-(titleText.height/2);
add(textEntity);
var splashText:Text = new Text("GAME NAME",0,0,640,480);
splashText.color = 0x00ff00;
splashText.size = 32;
var splashEntity:Entity = new Entity(0,0,splashText);
splashEntity.x = (FP.width/2)-(splashText.width/2);
splashEntity.y = 100;
add(splashEntity);
}
override public function update():void {
if (Input.check(Key.X)) {
FP.screen.color = 0x222233;
FP.world=new theWorld();
}
}
}
}
Basically here we need to display two texts: the game name and the classic “Press X to Start” you can see in most FlashPunk games. Then we need to detect when the player presses X and make him play.
Lines 10-14 add the “Press X to Start” text, using the Text
class and its constructor (line 10) with the text to be rendered. Then we need to create an Entity containing such text element (line 11), center the text in the middle of the screen (lines 12-13, quite easy to understand) and finally the Entity is added to the world (line 14).
We can also define the color of the text, like I did with the name of the game (line 16) and its size (line 17) but if we change its size making it bigger than the default value (16), we will need to define its width and height, in pixels (the latest two arguments at line 15).
And now we are done with the texts.
update
function at lines 23-28 checks for the player to press X and then changes screen color and initializes the world to the “game” world we met during step 2.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.