PDA

View Full Version : [AS3] Gameboard (NO AI) help


estupidyante
09-16-2010, 04:15 AM
I'm new on actionscript and want to create a boardgame similar to stratego, I read some articles and come out with this code:

var c1 = new ColorTransform(1,1,1,1,0,0,0,1);
var c2 = new ColorTransform(0,0,0,1,51,102,153,1);

// create the board
for(var c=0; c<8; c++) {
for(var r=c%2; r<9+c%2; r++) {
var square:Sprite = new Sprite();
var color:uint = (c < 4) ? 0x999999 : 0xFFFFFF;
square.graphics.beginFill(color);
square.graphics.lineStyle(1, 0x000000);
square.graphics.drawRect(0,0, 50, 50);
square.graphics.endFill();
square.x = (r-c%2) * 50;
square.y = c * 50;
square.addEventListener(MouseEvent.ROLL_OVER, rollover);
square.addEventListener(MouseEvent.ROLL_OUT, rollout);
square.addEventListener(MouseEvent.CLICK, controlBox);
addChild(square);
}
}
// change color of the box when rollover
function rollover(e:MouseEvent):void {
e.target.transform.colorTransform = c2;
}

function rollout(e:MouseEvent):void {
e.target.transform.colorTransform = c1;
}
var pmove:Sprite = new Sprite();
addChild(pmove);

var select:Sprite = new Sprite();
addChild(select);

function controlBox(e:MouseEvent):void {
// highlight the box selected
select.graphics.clear();
select.graphics.lineStyle(2, 0x999999);
select.graphics.beginFill(0x6EAE09, 0.8);
select.graphics.drawRect(e.target.x, e.target.y, e.target.width, e.target.height);
select.graphics.endFill();
// highlight the possible moves
pmove.graphics.clear();
pmove.graphics.beginFill(0x9CFF00, 0.3);
pmove.graphics.drawRect(e.target.x-50, e.target.y, e.target.width, e.target.height);
pmove.graphics.drawRect(e.target.x, e.target.y-50, e.target.width, e.target.height);
pmove.graphics.drawRect(e.target.x, e.target.y+50, e.target.width, e.target.height);
pmove.graphics.drawRect(e.target.x+50, e.target.y, e.target.width, e.target.height);
pmove.graphics.endFill();
}

how can I put my pieces on the board and setup their position (by swaping the pieces) after I setup the position if I click the piece it will suggest a move that allowed for that pieces (forward, backward, right, left)? what should I need to learn? or how should I do all of that?

Thanks,

TomMalufe
09-16-2010, 07:20 PM
For easy to follow and logical code you should really make this using Object Oriented code.

It's really easy to create your board class with it's 2D array of squares.
make a square class as well to hold the unit information or availability of the space.
make a unit class to hold general information about the unit (name, number, ID, image, etc)
finally make a base class that controls the flow and dynamics of the game (turns and battle calculations, win conditions, etc).

If I had to guess, it looks like you are writting your code on the timeline. A game is going to be too complex for that. Try using Flash Builder 4 to write your code. If you want you can make your classes into Components and do the layout in the Flash IDE.

estupidyante
09-17-2010, 08:46 AM
Thanks, I will try using Flash Builder 4.

rrh
09-17-2010, 04:19 PM
Right now, where you're setting up the "square" variable, you could store the r and c values in it, and store them in a 2D array like so:

boardArray = new Array();
for(var c=0; c<8; c++) {
boardArray.push(new Array());
for(var r=c%2; r<9+c%2; r++) {

square.c = c;
square.r = r;
boardArray[c].push(square);



That way, in your controlBox function, you can easily find out what row and column you clicked on, and check squares next to it like so:
boardArray[this.c][this.r+1]