Quantcast
Channel: Pixi.js Latest Topics
Viewing all articles
Browse latest Browse all 3978

Loader is async, right? So not sure how you'd structure this

$
0
0

If your game is in a JS file, and you do

var myGame = new CoolGame();

myGame.Init();

myGame.Start();

for example, then CoolGame does loader.add(...); a bunch of times, it's my understanding that it's asynchronous, hence the need to do .load to add a complete event.

In the code above though, I would not want my code to continue onto the next line and start the game before it had loaded.

So how can I make the loader block until it's done? It also makes for messy code:

function Init() {

    LoadTextures();

    PrepareSprites(); // Might error if textures not finished loading

    SetupGameMap();

}



would have to be
 

function Init() {

    LoadTextures();

}

function TexturesLoaded() { // Invoked with .load(fn) on the loader

    PrepareSprites();

    SetupGameMap(); // Doesn't really belong in this method

}

 

Perhaps I can do something like

 

while (!loader.Isfinished) { }

 

I'm not massively au fait with javascript so perhaps you are supposed to chain things by nesting them, but it seems wrong to me.


Viewing all articles
Browse latest Browse all 3978

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>