0

I'm working on a project in game development in Javascript and HTML5 canvas. I have this common code I use for loading sprites:

var sprite = new Image();
sprite.src = "sprite.png";

I was wondering if there was a simpler way to do this, which I first thought by function, but not sure how I should do so. I would think to do so like this:

function loadSprite(src) {
this.src = src;
}
var loadSprite(sprite.png);

However I don't think this is the right way to do it. Could someone correct my code and/or give a simpler way of loading an image like this? (I am also using a ctx.drawImage(..., sprite) in order to change coordinates on the canvas so it needs an x,y,width,and height parameters in one way or another)

1
  • 1
    well you need an image object.... and your second code does not have one... Commented Nov 20, 2018 at 14:31

1 Answer 1

1

Why not use as below:

function loadSprite(src) {
var sprite = new Image();
sprite.src = src;
return sprite
}

var _local_var = loadSprite('sprite.png');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.