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...