I'm trying to create a function that can dynamically create variables for adding sprites into a game. Here is what I have so far:
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)];
}
};
Josh Crozier
242k56 gold badges401 silver badges316 bronze badges
-
4Thank you for providing your code. Can you tell us what your problem is, or what you're stuck on exactly?Sampson– Sampson2012年11月18日 06:06:20 +00:00Commented Nov 18, 2012 at 6:06
-
My main problem is I have no idea how return the value so I can easily access it. e.g: newSprite("name", "image src"); getSprite("name"); Although I'm thinking about just make the and object.user1306988– user13069882012年11月19日 06:26:02 +00:00Commented Nov 19, 2012 at 6:26
1 Answer 1
How about creating sprite objects and push them into your sprite array?
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;
}
}
answered Nov 18, 2012 at 6:31
c 2
1,1653 gold badges13 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
BuddhiP
I also believe push is your friend here.
lang-js