Wait for Image to load ?

For developers using the Construct 2 Javascript SDK

Post » Thu Feb 09, 2012 5:46 pm

A problem I have with several plugin : how to force the runtime to load Image when I do something like a new Image() inside my plugin ?

Usually, we do something like :

[quote]
var img = new Image();
img.onload = function (){
ctx.drawImage(img);
}
img.src = http://myurl
[/quote]

But onload() doesn't fire. Is there a way to do that ?

I saw that some plugin for Scirra use
this.runtime.wait_for_textures.push(...);[/]
But it doesn't work either.

So I'm stuck...
B
33
S
9
G
6
Posts: 709
Reputation: 6,704

Post » Thu Feb 09, 2012 5:58 pm

don't use img.onload. It either won't fire or it will fire as soon as you change the SRC attribute, and there are no workarounds.
use img.completed, which is a flag that returns TRUE when the image is loaded. You'd have to check it every tick though. In my "insert external image into sprite frame" plugin (I can't come up with a good name), I use cf_fake_trigger and evaluate that flag every tick.Fimbul2012-02-09 17:59:30
B
36
S
8
G
8
Posts: 532
Reputation: 6,903

Post » Fri Feb 10, 2012 3:10 pm

You can use the runtime wait_for_textures, but it only works in the object type constructor, which runs just before the loading bar appears. During runtime you can't use it.
Scirra Founder
B
382
S
222
G
84
Posts: 23,984
Reputation: 189,235

Post » Fri Feb 10, 2012 3:32 pm

and what about jQuery(elem).load() ?
Passionate developer

If my work helped you to make your project, please consider making a donation :)
B
44
S
12
G
14
Posts: 631
Reputation: 10,987

Post » Fri Feb 10, 2012 6:14 pm

@Ashley : ok, so I think I'm going to use Fimbul's trick.
@septeven : it doesn't work either, because the jQuery .load() use .onload beneath it.
B
33
S
9
G
6
Posts: 709
Reputation: 6,704


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests