Creation of Flash game Space Checkers – Step 2
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
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<fieldArray.length; i++) { for (var j:int=0; j<fieldArray[i].length; j++) { square=new Square(); squareContainer.addChild(square); square.x=j*60; square.y=i*60; if (fieldArray[i][j]==1) { monster=new Monster(); monsterContainer.addChild(monster); monster.x=j*60; monster.y=i*60; monster.theRow=i; monster.theCol=j; monster.buttonMode=true; monster.addEventListener(MouseEvent.MOUSE_DOWN,pickMonster); } } } squareContainer.x=(stage.stageWidth-squareContainer.width)/2; squareContainer.y=(stage.stageHeight-squareContainer.height)/2; monsterContainer.x=squareContainer.x; monsterContainer.y=squareContainer.y; moveContainer.x=squareContainer.x; moveContainer.y=squareContainer.y; } private function pickMonster(e:MouseEvent):void { localX=e.localX; localY=e.localY; pickedMonster=e.target as Monster; oldX=pickedMonster.x; oldY=pickedMonster.y; checkMonster(pickedMonster); monsterContainer.setChildIndex(pickedMonster,monsterContainer.numChildren-1); stage.addEventListener(MouseEvent.MOUSE_MOVE,moveMonster); stage.addEventListener(MouseEvent.MOUSE_UP,dropMonster); } private function moveMonster(e:MouseEvent):void { pickedMonster.x=mouseX-monsterContainer.x-localX; pickedMonster.y=mouseY-monsterContainer.y-localY; } private function dropMonster(e:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveMonster); stage.removeEventListener(MouseEvent.MOUSE_UP,dropMonster); pickedMonster.x=oldX; pickedMonster.y=oldY; var i:int=moveContainer.numChildren; while (i--) { moveContainer.removeChildAt(i); } } private function checkMonster(m:Monster):void { var possibleMove:PossibleMove; var isPossible:Boolean=false; if (checkField(m,-1,0)) { possibleMove=new PossibleMove(); moveContainer.addChild(possibleMove); possibleMove.x=oldX; possibleMove.y=oldY-120; isPossible=true; } if (checkField(m,0,1)) { possibleMove=new PossibleMove(); moveContainer.addChild(possibleMove); possibleMove.x=oldX+120; possibleMove.y=oldY; isPossible=true; } if (checkField(m,1,0)) { possibleMove=new PossibleMove(); moveContainer.addChild(possibleMove); possibleMove.x=oldX; possibleMove.y=oldY+120; isPossible=true; } if (checkField(m,0,-1)) { possibleMove=new PossibleMove(); moveContainer.addChild(possibleMove); possibleMove.x=oldX-120; possibleMove.y=oldY; isPossible=true; } if (isPossible) { 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; } } } |
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.
They can be easily customized to meet the unique requirements of your project.





(5 votes, average: 4.80 out of 5)






This post has 2 comments
Creation of Flash game Space Checkers – Step 2 – Emanuele Feronato « eaflash
[...] on http://www.emanueleferonato.com Share this:TwitterFacebookLike this:LikeBe the first to like [...]
veeramani
thanks again…….