Making a game with flixel and Flash Builder 4 – Splash Screen

Did you read Creation of a game with flixel and Flash Builder 4?

Now it’s time to add a splash screen to the game. The splash screen or the title screen is the first thing the player sees, so we’ll add a backround image as well as the game title.

First, we must change our main file, HelloWorld.as, this way:

package {
	import org.flixel.*; //Allows you to refer to flixel objects in your code
	[SWF(width="520", height="240", backgroundColor="#000000")] //Set the size and color of the Flash file
	
	public class HelloWorld extends FlxGame
	{
		public function HelloWorld():void
		{
			super(260,120,splash_screen,2); //Create a new FlxGame object at 320x240 with 2x pixels, then load splash_screen
		}
	}
}

As you can see, the only change I made can be found at line 9… here I am calling splash_screen class, that is the brand new class we are going to create.

Here it is:

package
{
	import org.flixel.*;
	public class splash_screen extends FlxState
	{
		// embedding a png file given a relative path
		[Embed(source="../media/splash.png")] public var splash_image:Class;
		public function splash_screen():void
		{
			// declaring the sprite variable that will hold the splash image
			var splash:FlxSprite = new FlxSprite(0,0,splash_image);
			// this variable will hold all texts
			var my_text:FlxText
			// adding the splash image on state
			add(splash);
			// adding and styling the first text, the title itself
			my_text = new FlxText(0, 20, FlxG.width, "This is a Splash Screen")
			my_text.setFormat(null,16,0xFFFFFFFF,"center")
			add(my_text);
			// adding and styling a text saying the player has to press SPACE to play
			my_text = new FlxText(0, 80, FlxG.width, "Press SPACE to begin")
			my_text.setFormat(null, 8, 0xFFFFFFFF, "center");
			add(my_text);
		}
		override public function update():void
		{
			// checking if the player pressed SPACE...
			if (FlxG.keys.pressed("SPACE"))
			{
				FlxG.fade.start(0xff000000, 1, on_fade_completed);
				// ... if true then fade the screen to black and execute on_fade_completed function once the fade is completed
			}
			super.update();
		}
		public function on_fade_completed():void
		{
			// playing the game itself
			FlxG.state = new PlayState();
		}
	}
}

then PlayState class does not change…

package
{
	import org.flixel.*;
	public class PlayState extends FlxState
	{
		override public function create():void
		{
			add(new FlxText(0,0,100,"Hello, World!")); //adds a 100px wide text field at position 0,0 (upper left)
		}
	}
}

And this is the result:

Follow splash screen instructions to “play”… next time, the game itself

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers