function preload() {
this.load.image('scene', 'assets/LH.png');
this.load.spritesheet('woman', 'assets/woman.png', { frameWidth: 39, frameHeight: 38 });
this.load.spritesheet('diceButton1', 'assets/diceButton1.png', { frameWidth: 72, frameHeight: 20 });
this.load.spritesheet('movePlayer1', 'assets/movePlayer1.png', { frameWidth: 90, frameHeight: 30 });
}
function create() {
this.add.image(0, 0, 'scene').setOrigin(0, 0);
var woman = this.physics.add.sprite(1095, 430, 'woman').setInteractive();
var diceButton1 = this.add.sprite(350, 450, 'diceButton1').setInteractive();
var movePlayer1 = this.add.sprite(350, 550, 'movePlayer1').setInteractive();
diceButton1.on('pointerup', function() {
var dice1 = Math.floor(Math.random() *6) + 1;
var dice2 = Math.floor(Math.random() *6) + 1;
var dice3 = dice1 + dice2;
var diceText = this.add.text(320,470, 'Dice 1 ' + dice1 + 'Dice 2' + dice2, { fontSize: '16px', fill: '#000' });
}, this);
//Move player1 around the board
var player1 = {name: "", position: 0, money: 10000000, cycle:0};
player1.position += dice3;
if (player1.position > 40) {
player1.position = player1.position - 41
}
player1PixelPosition = [
[1095,430],[1045,410],[995,395], [945,385], [895,380],
[840,380], [785,390], [735,395], [680,395], [625,420],
[570,415], [520,410], [465,415], [415,415], [360,415],
[300,420], [250,420], [150,335], [150,280], [250,180],
[300,185], [350,165], [400,165], [440,165], [440,130],
[550,45], [610,50], [660,30], [710,30], [765,30],
[815,30], [875,30], [925,30], [980,30], [1060,70],
[1110,120],[1145,170],[1180,220],[1210,285],[1210,345],
[1025,390]
]
movePlayer1.on('pointerup', function() {
this.tweens.add({
targets: woman,
x: player1PixelPosition[player1.position][0],
y: player1PixelPosition[player1.position][1][0],
duration: 1000
});
}, this);
}
Should be player1PixelPosition[player1.position][1]
?
Hi Samme,
that was an incomplete edit before posting. [player1.PixelPosition[1] is what I had that did not work. If I take out these lines of code
player1.position += dice3;
if (player1.position > 40) {
player1.position = player1.position - 41
}
and I also replace the x and y coordinates with
x: 500,
y: 300,
The sprite moves when clicked
Unfortunately I’m still unable to solve the problem of moving the sprite with the click of a mouse
What’s the complete code now?
TheStreetHustler
body {
margin: 0;
}
Open the browser console and look for errors.
I moved the entire move player code into the dicebutton.on function and it fixed the problem. Thanks
diceButton1.on(‘pointerup’, function() {
var dice1 = Math.floor(Math.random() *6) + 1;
var dice2 = Math.floor(Math.random() *6) + 1;
var dice3 = dice1 + dice2;
var diceText = this.add.text(320,470, 'Dice 1 ' + dice1 + 'Dice 2' + dice2, { fontSize: '16px', fill: '#000' });
//Move player1 around the board
var player1 = {name: "", position: 0, money: 10000000, cycle:0};
player1.position += dice3;
if (player1.position > 40) {
player1.position = player1.position - 41
}
player1PixelPosition = [
[1095,430],[1045,410],[995,395], [945,385], [895,380],
[840,380], [785,390], [735,395], [680,395], [625,420],
[570,415], [520,410], [465,415], [415,415], [360,415],
[300,420], [250,420], [150,335], [150,280], [250,180],
[300,185], [350,165], [400,165], [440,165], [440,130],
[550,45], [610,50], [660,30], [710,30], [765,30],
[815,30], [875,30], [925,30], [980,30], [1060,70],
[1110,120],[1145,170],[1180,220],[1210,285],[1210,345],
[1025,390]
]
movePlayer1.on('pointerup', function() {
this.tweens.add({
targets: woman,
x: player1PixelPosition[player1.position][0],
y: player1PixelPosition[player1.position][1],
duration: 1000
});
}, this);
}, this);
}