Creation of Flash game Space Checkers – Step 3

Read all posts about "" game

Continuing with the creation of Space Checkers game, we are going to let monsters be eaten and removed from the board after a successful move. It’s a very important step because removing monsters from the board allows players to solve levels and progress through the game.

Starting with some theory, once the player picks a monster, the game highlights the possible moves, if any, as seen in step 2. We also need to store such possible moves somewhere, so I created a vector of points to store possible moves.

Once the player picks a monster, the vector is populated with possible moves, and once the monster is released somewhere in the board, I just need to check for the tile the monster landed on to be one in the vector of possible moves.

Then, since at each successful move a monster is eaten and must be removed from the board, we need to know which monster has to be removed. This is not a big problem, since we know the player just performed a successful move, we just need to know the direction the player moved the monster, and remove the monster which lies in the adjacent tile in the opposite side of the direction the player just moved.

A real world example: if the player moved the monster to the right, the monster to remove is the monster to the left of the tile where the moved monster landed.

Here is the source code:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	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;
		private var possibleLandings:Vector.;
		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();
			for (var i:int=0; i<4; i++) {
				var deltaRow:int=(1-i)*(i%2-1);
				var deltaCol:int = (2 - i) * (i % 2);
				if (checkField(m,deltaRow,deltaCol)) {
					possibleMove=new PossibleMove();
					moveContainer.addChild(possibleMove);
					possibleMove.x = oldX + 120 * deltaCol;
					possibleMove.y = oldY + 120 * deltaRow;
					possibleLandings.push(new Point(m.theRow+2*deltaRow,m.theCol+2*deltaCol));
				}
			}
			if (possibleLandings.length > 0) {
				var startMove:StartMove=new StartMove();
				moveContainer.addChild(startMove);
				startMove.x = oldX;
				startMove.y = oldY;
			}
			else {
				var wrongMove:WrongMove=new WrongMove();
				moveContainer.addChild(wrongMove);
				wrongMove.x = oldX;
				wrongMove.y = oldY;
			}
		}
		private function checkField(m:Monster,rowOffset:int,colOffset:int):Boolean {
			if (fieldArray[m.theRow + 2 * rowOffset] != undefined && fieldArray[m.theRow + 2 * rowOffset][m.theCol + 2 * colOffset] != undefined) {
				if (fieldArray[m.theRow + rowOffset][m.theCol + colOffset] == 1 && fieldArray[m.theRow + 2 * rowOffset][m.theCol + 2 * colOffset] == 0) {
					return true;
				}
			}
			return false;
		}
	}
}

Line 14: possibleLandings is the vector of points used to store possible landing tiles

Lines 104-130: I modified a bit checkMonster function to make it shorter. It works the same way, just look how I push possible moves into possibleLandings vector at line 115.

Lines 72-77: Checking if the current landing tile is inside possibleLandings vector. Look at line 73: unfortunately I could not use indexOf method as it does not work with variable passed by reference.

Lines 79-81: This is the old code to place the monster in its starting position if the move is not allowed.

Lines 82-98: code to execute if the move is allowed: monster position and properties are updated, as well as fieldArray values, then I determine the direction the monster moved, and remove the killed monster (lines 88-93).

And this is the result:

Now you can remove monsters by jumping over them. Next time we’ll see how to progress through levels.

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

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