Reputation: 458
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);
}
Upvotes: 1
Views: 48
Reputation: 7360
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);
}
Upvotes: 1
Reputation: 5812
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);
Upvotes: 3