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 tile1
: This item is a wall2
: This item is the place where to drop a crate3
: This item is the crate4
: This item is the player5
: 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...