Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Cocos2d-JS Game Development

You're reading from   Learning Cocos2d-JS Game Development Learn to create robust and engaging cross-platform HTML5 games using Cocos2d-JS

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781784390075
Length 188 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Emanuele Feronato Emanuele Feronato
Author Profile Icon Emanuele Feronato
Emanuele Feronato
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Hello World – A Cross-platform Game FREE CHAPTER 2. Adding Interactivity – The Making of a Concentration Game 3. Moving Sprites Around the Screen – An Endless Runner 4. Learn about Swipes through the making of Sokoban 5. Become a Musical Maestro 6. Controlling the Game with Virtual Pads 7. Adding Physics to Your Games Using the Box2D Engine 8. Adding Physics to Your Games Using the Chipmunk2D Engine 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime