user1306988
user1306988

Reputation: 43

Dynamically creating variables in javascript

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)];
    }
};

Upvotes: 0

Views: 133

Answers (1)

c 2
c 2

Reputation: 1167

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;
    }
}​

Upvotes: 1

Related Questions