先上代码:
cc.Class({
extends: cc.Component,
properties: {
bg1: {
default: null,
type: cc.Node,
},
bg2: {
default: null,
type: cc.Node,
},
roll_speed: 3,
},
update: function(dt) {
var parentHeight = this.node.parent.height;
this.bg1.y -= this.roll_speed;
this.bg2.y -= this.roll_speed;
if(this.bg1.y <= -parentHeight) {
// a
var bg2Height = this.bg2.y;
this.bg1.y = parentHeight + bg2Height;
}
if(this.bg2.y <= -parentHeight) {
// b
var bg1Height = this.bg1.y;
this.bg2.y = parentHeight + bg1Height;
}
},
});
说明:
1. a、b是为了去除黑边
2. 这是竖版滚动,需要两个背景图片拼接