The code I am about to write has nothing to do with Cocos2d-JS as it's just plain JavaScript, and explaining it would go beyond the scope of this book. I am just checking for legal moves and will move the player and the crates accordingly.
Everything is managed by the move
function, which will check for legal moves and update crates and player positions. The move
function has two arguments, deltaX
and deltaY
, which represent the amount of tiles the player is trying to move horizontally or vertically.
This means move(0,1)
will try to move the player up (0 tiles horizontally, 1 tile vertically), move(-1,0)
will try to move the player left, and so on.
The swipeDirection
function changes this:
function swipeDirection(){ var distX = startTouch.x - endTouch.x; var distY = startTouch.y - endTouch.y; if(Math.abs(distX)+Math.abs(distY)>swipeTolerance){ if(Math.abs(distX)>Math.abs(distY)){ if(distX>0){ move(-1,0); } else{ move(1...