Help with loader

Help with loader
0

Hello friends,

I have huge problem here with preloading.
I have 2 sound files: ogg and m4a. On preload I am determine if I am using iOS or somthing else. However it seems like the loadRes is loading the file after the game launches.

here is my code:

onLoad {
        let audioExtension = "m4a";

        switch (cc.sys.os) {
            case cc.sys.OS_IOS: audioExtension = "ogg"; break;
            default: audioExtension = "m4a"; break;
        }

        const audio = `sounds/audiosprite`;
        const data = `sounds/audiosprite`;


        cc.loader.loadRes(data, cc.JsonAsset, (err, json) => {
            this._data = json;
        });

        cc.loader.loadRes(audio, cc.AudioClip, (err, audioClip) => {
            this._audioClip = audioClip;
        });
}

start () {
       cc.audioEngine.play(this._audioClip, false);
       // NOT WORKING
}

Now this is bed for me because I really want to preload assets or sounds on the splash screen.

Please help and provide docs / samples / information of how to preload specific file in splash screen (or before game launch).

hi @hananht,
I had a similar situation where I had to load some assets before the game started. what I did was in loadRes, after this._data = json have a

cc.systemEvent.emit(“jsonLoaded”);

and in the second loadRes, after this._audioClip = audioClip have a

cc.systemEvent.emit(“audioClipLoaded”);

in the first scene, deactivate your ui initially, and activate it only after the two events fire. so, you can have in onLoad:

cc.systemEvent.on(“jsonLoaded”, function() {
this.jsonLoaded = true;
});

cc.systemEvent.on(“audioClipLoaded”, function() {
this.audioClipLoaded = true;
});

and update should be:

update(dt) {
if (this.jsonLoaded && this.audioClipLoaded && this.done == false) {
this.uiNode.active = true;
cc.audioEngine.play(loaderScript._audioClip, false);
this.done = true;
}
}

needless to say, set jsonLoaded, audioClipLoaded and done to false initially.

this is a workaround that works - there may be more solutions. oh I forgot…the need for this is that the loadRes works asynchronously and thus by the time you get to start the audio clip may not have loaded. hence you have to wait for the assets to load before you can activate your ui and play your audio.

hope this is of help.

I did exactly what you said. A good idea btw. However, I still want to show the splash screen.