cocos creator:背景无限滚动

代码地址:https://gitee.com/wolfdong7/bg_roll

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    bg1 = null;
    bg2 = null;
    cur_bg = null;
    speed = 200;
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.bg1 = this.node.getChildByName('repeat_bg1');
        this.bg2 = this.node.getChildByName('repeat_bg2');
        this.cur_bg = this.bg1;
    }

    start () {

    }

    update (dt) {
        var s = dt * this.speed;
        this.bg1.x -= s;
        this.bg2.x -= s;
        if (this.cur_bg.x <= -1024) { // 地图切换
            if(this.cur_bg == this.bg2) {
                this.bg2.x = this.bg1.x + 1024;
                this.cur_bg = this.bg1;
            }
            else {
                this.bg1.x = this.bg2.x + 1024;
                this.cur_bg = this.bg2;
            }
        }
    }
}

版权声明:本文为wolfdong7原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。