I load a lot of images in a boucle “for”, and i would like to delete each image when the user click on it. My code works, but only on the LAST image displayed : even if i touch the first image, it’s the last who disapear : how could i pass on parameter, the image i click on please ?
here is my code :
for (var a = 0; a < nbTetes; a++) {
var imgFalse = this.add.image(a * 50, Phaser.Math.Between(-500, 50), 'tete1');
imgFalse.setInteractive();
imgFalse.on('pointerdown', () => { //quand on clique sur l'élément recherché
console.log(imgFalse .x);//always display the x of the last image, even if i click on the first image
the issue is i can’t pass the image i clicked on it, the “imgFalse” is ecrased with the iteration…
You need to create an array of the images, you are displaying every image, but the imgFalse variable is being overwritten on every iteration, you should do it like this:
var imgFalse = [];
for (var a = 0; a < nbTetes; a++) {
imgFalse[a] = this.add.image(a * 50, Phaser.Math.Between(-500, 50), 'tete1');
imgFalse[a].setInteractive();
imgFalse[a].on('pointerdown', () => {
console.log(imgFalse[a].x);
this.tetes[a].setInteractive();
console.log(this.tetes[a].x);
this.tetes[a].on('pointerdown', function () { //quand on clique sur l'élément recherché
console.log(mainGame.tetes[a].x);//same with this.tetes[a].x