Jump to content

Chess game prototype


SteppingRazor
 Share

Recommended Posts

Hello,

I just discovered phaser. For my first test project I wanted to create some kind of 2d chess game. To make it simple, for my first task i decided to display board and a pawn. And assign to this pawn moving rules. As you all probably know pawns move forward by 1 tile and on their first move they may advance by 2 squares.

I managed to display sprites representing board and a pawn. I've enabled dragging to pawn sprite and I'm stuck. Now in my application you can drag the pawn and drop it anywhere, even outside the board.

I need any advice/hint how can I force the pawn to go back to previous position if the user drop it on wrong square. I didn't found any examples on this forum or on the front page, how can I achieve that. Can you help a little?

Link to comment
Share on other sites

Hi,

 

See this example.

http://phaser.io/examples/v2/input/drag-event-parameters

 

You can get which sprite you are dragging in onDragStart, and onDragStop.

So if you just add 2 functions, saving and loading the sate of the pawn, you can simply

function onDragStart(sprite, pointer) {    sprite.saveState();}function onDragStop(sprite, pointer) {    if( //pointing outside of the board) {      sprite.loadState();    }    else{      sprite.confirmState(pointer);    }}

Assuming your Pawn class has something like

Pawn.prototype.saveSate=function(){ // Save the current state}Pawn prototype.loadState=function(){  // recover the saved state}Pawn prototype.confirmState=function(pointer){  // update the state according the parameter}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...