abdel Posted November 29, 2019 Share Posted November 29, 2019 hy, I have an array of players Id that I should pass the corresponding one to a function when I click on a sprite. create () { var players = [123, 234, 222, 333]; var playButs = []; for (var i = 0; i < 4; i++) { playButs.push(null); } for (i = 0; i < 4; i++) { playButs[i] = this.add.sprite(70, 10*i+20, 'sheet', 'arrow'); playButs[i].setScale(1); playButs[i].setOrigin(0); playerId = players[i]; playButs[i].setInteractive().on('pointerdown', function(){this._play(playerId)}, this); } } _play(playerId){ console.log("PlayId After: "+playerId); //Always show 333 } the is that when I click on sprite the _play function get always the last item of players array instead on corresponding player. thank you Link to comment Share on other sites More sharing options...
Recommended Posts