[SOLVED] Code ability not working

Hello, this is my actual code

         for (var i=0; i<pl.ability.length; i++) {
                item=pl.ability[i];
                this.charAbPaper.rect(xOffset,yOffset,boxWidth,boxWidth).attr({
                    fill: '#000',
                    opacity: (pl.ability[i] !== 0 ? 1 : 0.5),
                    'stroke-width': 2,
                    stroke: '#fff'
                });
                var r = this.charAbPaper.image(this.inventory.icons[item.obj], xOffset,yOffset, boxWidth,boxWidth).attr({
                    opacity: (item !== 0 ? 1 : 0.5)
                });
                var self=this;
                r.data('item', item);
                r.click(function(e){
                    self.player.selAbility=item;
                    self.Root.script.Output.showAbility();
                });
                r.hover(function () {
                    self.player.clickable = false;
                    self.Root.script.Output.drawDetailsPanelForItem(this.data('item'));
                }, function() {
                    self.Root.script.Output.hideDetailsPanel();
                });
                xOffset+=60;
            }

but if more than 1 pl.ability in this line self.player.selAbility=item; the item is the last one of the array, how do i solve this?

You are basically doing this:

for(var i = 0; i < items.length; ++i)
{
  newItem = items[i];
  this.item = newItem;
}

uhm you mean that the r.click function is overwritten for each object with the last one? … i see

You are currently setting selAbility to be the same value as item on right click. What is the value of the item after the loop is done?

Yes that’s actually the last one of the pl.ability array, have to change the code :blush: thanks @yaustar

Variables are function scoped so you could use anonymous functions to do something like this (untested but should work ™).

Change:

r.data('item', item);
r.click(function(e){
    self.player.selAbility=item;
    self.Root.script.Output.showAbility();
});
function(abilityItem) {
    r.data('item', abilityItem);
    r.click(function(e){
        self.player.selAbility=abilityItem;
        self.Root.script.Output.showAbility();
    });
}(item);

You are right…i have the same code in another script :stuck_out_tongue: works fine now thanks!