dimanche 19 avril 2015

Javascript images loading function hangs up my browser

I'm trying to make simple game in Javascript and HTML so I need images function to load sprites. I'd like it to wait until image is loaded and then proceed to another image. Unfortunatly code I wrote stops my browser from working. I think that it's endless while loop that should be finished.


Here's my code:



var Sprites = {};
function NewSprite( Name, URL )
{
Sprites[ Name ] = new Image( );
Sprites[ Name ].Ready = false;
Sprites[ Name ].onload = function ( )
{
this.Ready = true;
}
Sprites[ Name ].src = URL;

while ( Sprites[ Name ].Ready === false );
}


When I run it without while loop at the end and check Sprites[ Name ].Ready value it gives me true so I think it should work.


I'm calling my function like this:



NewSprite( "img", "http://ift.tt/1D2GxhK" );


Thanks for help!


Aucun commentaire:

Enregistrer un commentaire