SteppingRazor Posted December 6, 2015 Share Posted December 6, 2015 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 More sharing options...
Gyutang Posted December 6, 2015 Share Posted December 6, 2015 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 simplyfunction 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 likePawn.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 More sharing options...
SteppingRazor Posted December 8, 2015 Author Share Posted December 8, 2015 Thanks man. That is what I was looking for. Link to comment Share on other sites More sharing options...
Recommended Posts