Talking about Space Checkers game, Actionscript 3, Flash and Game development.
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.