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