简体   繁体   English

功能和图像导入

[英]Functions, and Image importing

I'm working on a project in game development in Javascript and HTML5 canvas. 我正在用Javascript和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) (我也使用ctx.drawImage(...,sprite)来更改画布上的坐标,因此它需要以一种或另一种方式使用x,y,width和height参数)

Why not use as below: 为什么不按以下方式使用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM