layabox 每隔一段时间 生成一个物体 代码教程

分类栏目:layabox教程

187

如何在layabox 每隔一段时间 生成一个物体

只需要在js代码里面加上 下面代码即可



   onUpdate() {
        //每间隔一段时间创建一个盒子
        let now = Date.now();
        if (now - this._time > this.createBoxInterval) {
            this._time = now;
            this.createBox();
        }
    }
    createBox() {
        //使用对象池创建盒子
        let box = Laya.Pool.getItemByCreateFun("dropBox", this.dropBox.create, this.dropBox);
        box.pos(Math.random() * (Laya.stage.width - 100), -100);
        this._gameBox.addChild(box);

    }

主要使用getItemByCreateFun 函数

dropBox 是预制件的名称