How to make update wait until loadRes ended?

Hi,
I’m using loadRes in onLoad function.

onLoad: function () {
    let url = 'prefab/bullet_prefab';
    cc.loader.loadRes(url, cc.Prefab,
        function (err, prefab) {
            if (err) {
                cc.error(err.message || err);
                return;
            };
            cc.log('Result should be a prefab: ' + (prefab instanceof cc.Prefab));
            this.bullet = cc.instantiate(prefab);
        });
}

Then I trying to use the prefab in update.

update: function (dt) {
    this.bullet.setPosition(0, 0);
    //
}

But loadRes is not finished yet when update started.
How can I make update wait until loadRes ended?
Thanks.

Hi PaPiPuGames,

add a variable this.loaded = false;, set it at the end of loadRes to true, and check in update if (this.loaded){ this.bullet... }.

Cheers

Mike

1 Like

Hi Mike,
Thank you for the advice.
I consider the issue as too difficult. But the solution was really simple.

But I realised that I better leave reasource loading to “cocos creator”.
Thanks.
-Hiroki