I have this error every time I try to convert my code into a function
Uncaught TypeError: Cannot read properties of undefined (reading ‘sprite’)
var diceImages = [‘di1’, ‘di2’, ‘di3’, ‘di4’, ‘di5’, ‘di6’];
function preload() {
//load the dice images
for (var i = 0; i < diceImages.length; i++) {
this.load.image(diceImages[i], ‘assets/’ + diceImages[i] + ‘.png’);
}
function rollDice(){
dice1 = Math.floor(Math.random() * 6) + 1;
dice2 = Math.floor(Math.random() * 6) + 1;
dice3 = dice1 + dice2;
// Get the names of the images to display based on the dice rolls
const image1 = diceImages[dice1 - 1];
const image2 = diceImages[dice2 - 1];
// Display the images on the screen
const firstDi = this.add.sprite(240, 230, image1); //<== error is here at .sprite
const secondDi = this.add.sprite(320, 230, image2);
// Define the animation duration and number of rotations
const duration = 1000; // in milliseconds
const numRotations = 8;
// Define the animation properties for both images
const animationProps = {
angle: 360 * numRotations,
duration: duration,
ease: 'Linear',
onComplete: function() {
// Re-enable the button after animation completes
// diceButton1.setInteractive();
}
};
// Apply the animation to both images simultaneously
this.tweens.add({
targets: [firstDi, secondDi],
...animationProps
});
function create () {
diceButton1.on(‘pointerup’, function () {
rollDice(); //<== this is where I calleed the function
}, this)
}