0

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)];
    }
};
2
  • 4
    Thank you for providing your code. Can you tell us what your problem is, or what you're stuck on exactly? Commented 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. Commented Nov 19, 2012 at 6:26

1 Answer 1

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;
    }
}​
Sign up to request clarification or add additional context in comments.

1 Comment

I also believe push is your friend here.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.