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

Building a level


Normally, tile-based levels are stored in two-dimensional arrays, and Cocosban follows this trend. So, the first global variable we'll declare in gamescript.js, which is an array containing level data, is as follows:

var level = [
  [1,1,1,1,1,1,1],
  [1,1,0,0,0,0,1],
  [1,1,3,0,2,0,1],
  [1,0,0,4,0,0,1],
  [1,0,3,1,2,0,1],
  [1,0,0,1,1,1,1],
  [1,1,1,1,1,1,1]
];

Each item represents a tile, and each value represents an item, which I coded this way:

  • 0: This item is an empty tile

  • 1: This item is a wall

  • 2: This item is the place where to drop a crate

  • 3: This item is the crate

  • 4: This item is the player

  • 5: This item is the crate on a place where to drop a crate (3+2)

  • 6: This item is the player on a place where to drop a crate (4+2)

Our gameScene declaration is always the same:

var gameScene = cc.Scene.extend({
  onEnter:function () {
  this._super();
    gameLayer = new game();
    gameLayer.init();
    this.addChild(gameLayer);
  }
});

And finally, we are ready to extend the game...