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

Adding an endless scrolling background


Now, it's time to add the cityscape background, which will scroll endlessly and seamlessly. Finally, you can start editing gamescript.js:

var background;
var gameLayer;
var scrollSpeed = 1;
var gameScene = cc.Scene.extend({
  onEnter:function () {
    this._super();
    gameLayer = new game();
    gameLayer.init();
    this.addChild(gameLayer);
  }
});
var game = cc.Layer.extend({
  init:function () {
    this._super();
    background = new ScrollingBG();
    this.addChild(background);
    this.scheduleUpdate();
  },
  update:function(dt){
    background.scroll();
  }
});
var ScrollingBG = cc.Sprite.extend({
  ctor:function() {
    this._super();
    this.initWithFile("assets/background.png");
  },
  onEnter:function() {
    this.setPosition(480,160);
  },
  scroll:function(){
    this.setPosition(this.getPosition().x-scrollSpeed,this.getPosition().y);
    if(this.getPosition().x<0){
      this.setPosition(this.getPosition().x+480,this.getPosition...