1

Hello what i am trying to do is just to draw few images .so at first i made a group(i think this function is a part of a plugin i am using but thats no matter) then i made an array to store a name and src of several images and then i am calling a for loop to draw those images but the thing is i got no results at all no errors nothing so i try to alert "b.name" and "b.src" but result i got is undefined could someone explain me where is a problem cuz for me its seems everything good

            var mapGroup = new Group();
            var mapVillage = [
                {name: "leaf", src: "js/leaf.png"},
                {name: "sand", src: "js/sand.png"},
                {name: "stone", src: "js/stone.png"},
                {name: "cloud", src: "js/cloud.png"},
                {name: "mist", src: "js/mist.png"},
                {name: "rain", src: "js/rain.png"},
                {name: "grass", src: "js/grass.png"},
                {name: "sound", src: "js/sound.png"},
                {name: "hotwater", src: "js/hotwater.png"},
                {name: "waterfall", src: "js/waterfall.png"}
            ];

            for(var i = 0; i < mapVillage.length; i++){
                var b = mapVillage[i];
                b = new Sprite(game.width, game.height);
                b.image = game.assets[b.src];
                alert(b.name);
                mapGroup.addChild(b);
            }
3
  • 1
    You're overriding b in var b = mapVillage[i]; b = new Sprite(game.width, game.height); Commented Jul 12, 2016 at 12:07
  • The variable b cannot be both an entry in the mapVillage array and a new Sprite at the same time. Commented Jul 12, 2016 at 12:07
  • i think i was trying to assing a sprite for each array element cuz later i am going to do hittest Commented Jul 12, 2016 at 12:15

2 Answers 2

3

Your mistake is here

   var b = mapVillage[i];
   b = new Sprite(game.width, game.height);

You are assigning b first to an entry of mapVillage, which does have the b.image you are looking for. But you are overriding it in the next line. So when you are calling b.image next, it is looking for 'image' on a "Sprite", which does not have that property ;-)

To solve it, simply assign the Sprite to a new variable. var sprite = new Sprite(game.width, game.height);

Sign up to request clarification or add additional context in comments.

Comments

1

You first assign to var b an object (var b = mapVillage[i];) then override it assigning a Sprite to it. You need two variables, something like this:

for (var i = 0; i < mapVillage.length; i++){
  var b = mapVillage[i];
  var sprite = new Sprite(game.width, game.height);
  sprite.image = game.assets[b.src];
  alert(b.name);
  mapGroup.addChild(sprite);
}

Comments

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.