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

Shuffling the tiles and adding the score


You should have noticed the game isn't that hard, since you are just matching tiles that are one next to each other. The first tile matches the second tile, the third tile matches the fourth, and so on.

First, you need to shuffle the tiles, then you will add the score to the game. Players love to compete for high scores.

You start by adding two new variables scoreText and moves, which will handle the text showing the score and count the number of moves (picks) the player did:

Var gameArray = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7];
var pickedTiles = [];
var scoreText;
var moves=0;

Then, you need to shuffle gameArray. Shuffling arrays with a true randomization is beyond the scope of this book, so for this game, you are going to use a basic shuffle function you can find at http://jsfromhell.com/array/shuffle:

var shuffle = function(v){
for(var j, x, i = v.length; i; j = parseInt(Math.random() * i),x = v[--i], v[i] = v[j], v[j] = x);
return v;
};

Then gameArray...