Book Image

Learning Cocos2d-JS Game Development

By : Emanuele Feronato
Book Image

Learning Cocos2d-JS Game Development

By: Emanuele Feronato

Overview of this book

Table of Contents (18 chapters)
Learning Cocos2d-JS Game Development
Credits
Foreword
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Creating Your Own Blockbuster Game – A Complete Match 3 Game
Index

Completing the game


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...