我正在尝试创建一个可以动态创建变量的函数,以便将精灵添加到游戏中。以下是我到目前为止的情况:
function SpriteSetUp(name,src){
var Sprites = [];
var i = Sprites.length;
if(Sprites[Sprites.indexOf(name)] == name){
return Sprites[Sprites.indexOf(name)];
}else{
Sprites[i] = name;
Sprites[i].src = src;
return Sprites[Sprites.indexOf(name)];
}
};
答案 0 :(得分:1)
如何创建精灵对象并将它们推入精灵数组?
function SpriteContainer() {
this.sprites = [];
this.addSprite= function(name, src) {
var newSprite = new Sprite(name,src);
sprites.push(newSprite);
}
function Sprite(name, src) {
this.name = name;
this.src = src;
}
}