Reuse your Flash games code (as much as you can!)

Let me guess something: when you are designing your latest game, you start with a working prototype, design a couple of levels, maybe add some cute graphics then… in most case you abandon the project.

Since it happens quite often to me, I tried to focus on what is making me drop a project.

And I found once I have an almost working game, I get bored when it’s time to add splash screen, buttons, music, level selection, third part APIs, and all that stuff which isn’t related to the game itself.

So I decided to build some kind of framework/template which will do the boring job for me, letting me focus on what’s really importat: making games.

Look at these level selection screens:

I made them in a minute, just changing some variables in my template.

Let me explain the concept: this is the class of the level selection screen itself:

package {
	import flash.display.Sprite;
	public class levelSelectionMc extends Sprite {
		public function levelSelectionMc(beaten:int):void {
			var setup:setupClass = new setupClass();
			var levelSelect:levelSelectMc;
			for (var i:int=0; i

As you can see, I have the amount of beaten levels as argument at line 4, then a for loop which iterates through the entire number of levels (defined in a class called setupClass) and constructs levelSelectMc instances (the level thumbnail) passing as arguments the number of the level and a Boolean variable which is true if the level has already been beaten, false otherwise.

Then, levelSelectMc class is defined as follows:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	public class levelSelectMc extends Sprite {
		private var level:Number;
		public function levelSelectMc(n:Number,solved:Boolean):void {
			level=n;
			var setup:setupClass = new setupClass();
			if (! solved) {
				var locked:lockedMc = new lockedMc();
				addChild(locked);
			}
			else {
				addEventListener(MouseEvent.CLICK,clicked);
			}
			var levelWidth:Number=setup.levelsPerRow*width+(setup.levelsPerRow-1)*setup.levelsHorizontalSpacing;
			var numberOfRows:Number=Math.floor(setup.totalLevels/setup.levelsPerRow);
			var levelHeight:Number=numberOfRows*height+(numberOfRows-1)*setup.levelsVerticalSpacing;
			var xOffset:Number=(setup.gameWidth-levelWidth)/2+setup.levelsOffsetX;
			var yOffset:Number=(setup.gameHeight-levelHeight)/2+setup.levelsOffsetY;
			x=n%setup.levelsPerRow*(width+setup.levelsHorizontalSpacing)+xOffset;
			y= Math.floor(n/setup.levelsPerRow)*(height+setup.levelsVerticalSpacing)+yOffset;
			leveltext.text=(n+1).toString();
			mouseChildren=false;
			buttonMode=true;
		}
		private function clicked(e:MouseEvent):void {
			removeEventListener(MouseEvent.CLICK,clicked);
			var theParent:Main=this.parent.parent as Main;
			theParent.playGame(level);
		}
	}
}

As you can see at lines 16-23, the position of the level thumbnail varies according to the number of thumbnails per row, the horizontal and vertical spacing between thumbnails, and absolute offsets, which are also defined in setupClass class.

So, changing some basic values in this class:

package {
	public class setupClass {
		// basic game info
		public var gameWidth:Number=640;
		public var gameHeight:Number=480;
		// levels
		public var totalLevels:Number=40;
		public var levelsPerRow:Number=8
		public var levelsHorizontalSpacing:Number=5;
		public var levelsVerticalSpacing:Number=5;
		public var levelsOffsetX:Number=0;
		public var levelsOffsetY:Number=30;
		// play game button
		public var playGameButtonX:Number=320;
		public var playGameButtonY:Number=230;
		// back to menu button
		public var homeButtonX:Number=560;
		public var homeButtonY:Number=0;
		// level completed
		public var completedReplayButtonX:Number=320;
		public var completedReplayButtonY:Number=200;
		public var completedPlayNextButtonX:Number=320;
		public var completedPlayNextButtonY:Number=240;
		public var completedShowLevelsButtonX:Number=320;
		public var completedShowLevelsButtonY:Number=280;
		// level failed
		public var failedReplayButtonX:Number=320;
		public var failedReplayButtonY:Number=320;
		public var failedShowLevelsButtonX:Number=320;
		public var failedShowLevelsButtonY:Number=360;
		
	}
}

I can create any kind of layout in a minute.

A lot of more stuff in the game is defined with the same concept, from statistics to sound button, from congratulations screen to game over... just to let me develop "plug and play" games.

Once my first couple of games will be released, I plan to give the template for free, to let you create and publish your games focusing only on game coding.

Did you already made something similar? Which stuff do you hate to insert in your game? Buttons? APIs? Let me know and I'll add them as much as I can.

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

214 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
// 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