This is a pared down version of the behaviour I am experiencing:
var target, gallery = [], counter;
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scene: {
create: create,
}
};
var game = new Phaser.Game(config);
function create ()
{
target = this.add.text(80,100,"0", {fontFamily: 'Arial', fontSize: 40, color: '#fff'}).setOrigin(0.5, 0.5);
for (counter = 1; counter <= 6; counter++) {
gallery[counter] = this.add.text( 80 + counter * 100, 100, counter,
{fontFamily: 'Arial', fontSize: 40, color: '#fff'}).setOrigin(0.5, 0.5);
gallery[counter].setInteractive().on('pointerdown',
() => { target.setText = counter, console.log(counter)});
}
}
Two problems here:
First, what I would like to happen is that the 0 gets replaced by whatever number gets clicked. That doesn’t happen. Why, what did I do wrong?
Second, the number that gets sent to the console is not the number that gets clicked, it’s actually 7. Which is what the counter variable ends up being after the end of the for loop. So how do I get the gallery items to remember the input from within the for loop, instead of referring back to the variable as it is after the for loop?
Thanks for any help, I am noob at Phaser.