Search code examples
javascriptgame-maker

Adding image with JavaScript


To make a long story short, I need to use JavaScript to add an image to an html5 game given the URL. The catch is that it is inside a program called gamemaker html5, which is basically a drag and drop IDE for creating HTML games.

I don't know any JavaScript, and simply need is code to add an image given the URL. Can someone please translate the following html into JavaScript for me?

<img src="http://www.mydomain.com/myimagefile.gif"></img>

This is all contained on an html5 canvas, so I need to be able to pop the image onto x,y coordinates.


Solution

  • To add an image to a GameMaker:HTML5 game, use this:

    newimg = sprite_add("http://www.mydomain.com/myimagefile.gif",1,false,false,0,0);
    

    Replace the arguments as necessary, but be aware that this does not support animated GIF files - they must be a strip if you want to import an animated sprite.

    newimg can then be used anywhere you would normally name a sprite, such as:

    draw_sprite(newimg,-1,32,32);