PDA

View Full Version : [AS3] Chess game


Ryan_28
09-26-2009, 08:24 AM
I'm working a flash chess game and ive managed to create the board, pieces and code that allows the pieces to be moved around the board. However, I'm still new to flash and programming in general and i dont know how to write the code that will make the chess pieces dissapear when they are taken by the opponent. Here is my code (albeit the code i ripped from the tutorial in flash help)
function restartMovie(event:MouseEvent):void
{
this.gotoAndStop(1);
}
playButton.addEventListener(MouseEvent.CLICK, restartMovie);

import flash.display.DisplayObject;
import flash.events.MouseEvent;
var offsetX:Number;
var offsetY:Number;
var draggedObject:DisplayObject;

function startDragging(event:MouseEvent):void

{
draggedObject = DisplayObject(event.target);

offsetX = event.stageX - draggedObject.x;
offsetY = event.stageY - draggedObject.y;


stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
function stopDragging(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
function dragObject(event:MouseEvent):void
{
draggedObject.x = event.stageX - offsetX;
draggedObject.y = event.stageY - offsetY;


event.updateAfterEvent();

}
pawn_black_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_3.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_3.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_4.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_4.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_5.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_5.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_6.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_6.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_7.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_7.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_8.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_8.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_3.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_3.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_4.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_4.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_5.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_5.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_6.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_6.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_7.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_7.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_8.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_8.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_black_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_black_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_black_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_black_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_white_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_white_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_white_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_white_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

knight_black_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_black_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

knight_black_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_black_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

knight_white_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_white_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

knight_white_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_white_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_black_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_black_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_black_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_black_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_white_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_white_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_white_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_white_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

queen_black.addEventListener(MouseEvent.MOUSE_DOWN , startDragging);
queen_black.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

queen_white.addEventListener(MouseEvent.MOUSE_DOWN , startDragging);
queen_white.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

king_black.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
king_black.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

king_white.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
king_white.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
stop();

if you would like to see the chess board and how it functions at current you can see it http://www.zeldaocarina.co.uk just scroll to the bottom of the website where you will see the chess game.
If anyone can help me out with this i will be very grateful.
Thank you

Ryan_28
09-26-2009, 09:50 AM
I'm working a flash chess game and ive managed to create the board, pieces and code that allows the pieces to be moved around the board. However, I'm still new to flash and programming in general and i dont know how to write the code that will make the chess pieces dissapear when they are taken by the opponent. Here is my code (albeit the code i ripped from the tutorial in flash help)
function restartMovie(event:MouseEvent):void
{
this.gotoAndStop(1);
}
playButton.addEventListener(MouseEvent.CLICK, restartMovie);

import flash.display.DisplayObject;
import flash.events.MouseEvent;
var offsetX:Number;
var offsetY:Number;
var draggedObject:DisplayObject;

function startDragging(event:MouseEvent):void

{
draggedObject = DisplayObject(event.target);

offsetX = event.stageX - draggedObject.x;
offsetY = event.stageY - draggedObject.y;


stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
function stopDragging(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject);
}
function dragObject(event:MouseEvent):void
{
draggedObject.x = event.stageX - offsetX;
draggedObject.y = event.stageY - offsetY;


event.updateAfterEvent();

}
pawn_black_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_3.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_3.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_4.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_4.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_5.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_5.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_6.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_6.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_7.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_7.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_black_8.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_black_8.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_3.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_3.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_4.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_4.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_5.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_5.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_6.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_6.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_7.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_7.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

pawn_white_8.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
pawn_white_8.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_black_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_black_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_black_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_black_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_white_1.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_white_1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

rook_white_2.addEventListener(MouseEvent.MOUSE_DOW N, startDragging);
rook_white_2.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

knight_black_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_black_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

knight_black_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_black_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

knight_white_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_white_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

knight_white_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
knight_white_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_black_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_black_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_black_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_black_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_white_1.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_white_1.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

bishop_white_2.addEventListener(MouseEvent.MOUSE_D OWN, startDragging);
bishop_white_2.addEventListener(MouseEvent.MOUSE_U P, stopDragging);

queen_black.addEventListener(MouseEvent.MOUSE_DOWN , startDragging);
queen_black.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

queen_white.addEventListener(MouseEvent.MOUSE_DOWN , startDragging);
queen_white.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

king_black.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
king_black.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

king_white.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
king_white.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
stop();

if you would like to see the chess board and how it functions at current you can see it http://www.zeldaocarina.co.uk just scroll to the bottom of the website where you will see the chess game.
If anyone can help me out with this i will be very grateful.
Thank you

RogerClark
09-26-2009, 10:03 AM
Hi Ryan_28,

You can use the removeChild method to remove a MC (or Sprite etc) from the display list.
If you want to fee up its memory you're also going to have to set the MC to null so that the garbage collector knows that you've finished with it.

e.g. to manually remove pawn_white_5 youd do

this.removeChild(pawn_white_5);
pawn_white_5=null;

Obviously you're going to have to determine the MC that you droped your piece on programatically

Ryan_28
09-26-2009, 12:07 PM
Thank you, the code works good but... as you said I'm going to have to determine the MC i dropped my piece on programmitically. Coud i use the if statement for this?
I clearly have a lot to learn :o
many thanks for your help

rrh
09-26-2009, 06:48 PM
I see you aren't yet snapping the piece to the square upon which it is placed. This strikes me as important, since if you can identify that the player moved the piece to a square that's occupied by the opposing player.

Make a 2D array for all the square positions.
So, when you drop a piece, snap to the nearest square and identify the position on the board and which entry in the array it corresponds with. If an opposing piece already exists on that square, remove it. Then add the dropped piece into the new position in the array and remove it from the old.