Creation of Flash game Space Checkers – Step 2

Read all posts about "" game

Here we go with the second part of the step by step Space Checkers tutorial. In the previous step I showed you how to initialize the game and move the monsters, now it’s time to develop game logic checking if the player picked a monster which can be moved and eventually showing where the monster can be placed.

In the original game when the player picks a monster, a red marker is placed over its tile.

If the monster can be moved, a cyan marker is placed over its tile, and one or more yellow markers are placed on the tiles the monster can be placed on.

This is what we are going to create:

Pick a monster, and see the tiles highlight according to the rules mentioned above.

Three new symbols have been created, called StartMove, PossibleMove and WrongMove, representing respectively the highlights for the starting tile, the possible move tiles and the highlight for a wrong move.

Here’s how I changed the script:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	public class Main extends Sprite {
		private var fieldArray:Array;
		private var pickedMonster:Monster;
		private var monsterContainer:Sprite=new Sprite();
		private var moveContainer:Sprite=new Sprite();
		private var localX:Number;
		private var localY:Number;
		private var oldX:Number;
		private var oldY:Number;
		public function Main() {
			setupLevel();
		}
		private function setupLevel():void {
			var squareContainer:Sprite=new Sprite();
			addChild(squareContainer);
			addChild(moveContainer);
			addChild(monsterContainer);
			var square:Square;
			var monster:Monster;
			fieldArray=[[0,0,1,0,0,0],[1,1,0,1,1,0],[0,0,0,0,0,0]];
			for (var i:int=0; i

Let me explain the most interesting parts:

Line 7: a new sprite called moveContainer is created. It will contain all highlights

Line 19: moveContainer is added between squareContainer and monsterContainer. This is important as it affects the hierarchy of Display Objects. Highlights will stay in front of the tiles, and monsters will stay in front of highlights.

Lines 35-36: adding two custom properties to monster, called theRow and theCol, storing monster's row and column.

Line 55: the core of this script: once a monster is picked, checkMonster functions will handle highlights.

checkMonster function (lines 74-117)needs to be optimized, but it just checks for possible moves at the top, left, bottom and right of the picked monster, placing the right highlights according to monster capabilities.

The really interesting function is checkField (lines 118-125) which receives as arguments the picked monster, the row offset to watch (+1 = down, -1 = up) and the column offset to watch (+1 = right, -1 = left).

Line 119: to check if it's a valid move, we can look two tiles further. The first tile must be a monster, and the second tile must be an empty tile. But first, we need to check if tiles exist. This statement checks if tiles exist

Line 120: Now it's time to see if the first tile is a monster and the second tile is an empty tile.

Line 121: returns true: it's a valid move

Line 124: returns false: it's an illegal move

Next time, killing monster and completing the level.

Download the source code.

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