Could anyone please help me with this project? The issue starts on task 19. From then on I cannot seem to get my code to work
My code:
// Global variables
let plot;
let grid = [];
let coinCollection = [];
let playerCoins = 0;
let compCoins = 0;
class GameScene extends Phaser.Scene {
constructor() {
super({
key: "GameScene",
});
}
preload() {
// We have preloaded our images such as the background, the coins, the digging image etc.
this.load.image(
"board",
"https://content.codecademy.com/courses/learn-phaser/Treasure%20Hunter/Gameboard%20Default.png",
{
frameWidth: 240,
frameHeight: 320,
}
);
this.load.image(
"gold",
"https://content.codecademy.com/courses/learn-phaser/Treasure%20Hunter/gold%20coin.png"
);
this.load.image(
"playerGold",
"https://content.codecademy.com/courses/learn-phaser/Treasure%20Hunter/gold%20coin%20shine.png"
);
this.load.image(
"dig",
"https://content.codecademy.com/courses/learn-phaser/Treasure%20Hunter/dug%20hole.png"
);
this.load.image(
"blank",
"https://content.codecademy.com/courses/learn-phaser/Treasure%20Hunter/blank.png"
); // These are the actual images / tiles that load on top of 'board'
}
create() {
// The background is provided below
this.add.sprite(240, 320, "board");
// Create the for loops to create the grid
for (let x = 46; x < 437; x += 78) {
// nested for loop to create 6 rows
for (let y = 46; y < 437; y += 78) {
// create blank sprites at each x, y coordinate
plot = this.add.sprite(x, y, "blank");
// add each plot to the grid to track it
grid.push(plot);
}
}
// Function to decide random coin locations
function decideCoinLocations() {
// while loop to collect 5 random coin locations
while (coinCollection.length < 5) {
// generate a random index between 0 and 35
let randIdx = Math.floor(Math.Random() * grid.length);
// select plot at the random index
let selectedPlot = grid[randIdx];
// only add selected plot to coin collection if it's not already there
if (coinCollection.indexOf(selectedPlot) === -1) {
coinCollection.push(selectedPlot);
}
}
}
// call the function to populate coinCollection
decideCoinLocations();
// Choose random indices from 2d array
} // End of Create
update() {
// Determine winner and scores
let determineWInner = this.determineWinner();
let playerScore = this.add.text(195, 487, " " + playerCoins + " ", {
font: "16px Helvetica",
fill: "#000000",
padding: {
x: 6,
y: 7,
},
backgroundColor: "#ffffff",
});
let compScore = this.add.text(430, 487, " " + compCoins + " ", {
font: "16px Helvetica",
fill: "#000000",
padding: {
x: 6,
y: 7,
},
backgroundColor: "#ffffff",
});
// Input / marker creation
this.input
.setHitArea(grid)
.on("gameobjectover", function (pointer, gameObject) {
gameObject.setTint(0xff0000);
});
this.input
.setHitArea(grid)
.on("gameobjectout", function (pointer, gameObject) {
gameObject.clearTint();
});
//Check on player click
this.input
.setHitArea(grid)
.on("gameobjectdown", function (pointer, gameObject) {
if (gameObject.texture.key === "blank") {
game.input.enabled = false;
let clickedPlot = gameObject;
//playerCheck starts here
function playerCheck() {
// iterate through coinCollection
for (let i = 0; i < coinCollection.length; i++) {
// check if clicked plot matches the current coin in coinCollection
if (clickedPlot === coinCollection[i]) {
// if player found a coin
playerCoins++; // increment player score
clickedPlot.setTexture("playerGold"); // change sprite to gold
break; // end loop
} else if (i === coinCollection.length - 1) {
// if player did not find coin
clickedPlot.setTexture("dig");
}
}
// Wrap this in your if statement
setTimeout(function () {
compTurn();
}, 450);
}
playerCheck();
} else {
function input() {
game.input.enabled = true;
}
setTimeout(function () {
input();
}, 1000);
}
});
// Computer turn
function compTurn() {
let compChoice;
let item;
let items = [];
function singleIndex() {
item = Math.floor(Math.random() * grid.length);
for (let i = 0; i < 1; i++) {
if (items.indexOf(item) !== null) {
compChoice = {
c: grid[item],
key: grid[item].texture.key,
};
items.push(item);
} else {
singleIndex();
}
}
}
singleIndex();
if (compChoice.key !== "blank") {
compTurn();
} else {
for (let i = 0; i < coinCollection.length; i++) {
if (compChoice.c === coinCollection[i]) {
compCoins++;
grid[item].setTexture("gold");
break;
} else {
grid[item].setTexture("dig");
}
}
}
function input() {
game.input.enabled = true;
}
setTimeout(function () {
input();
}, 1000);
}
} //end Update
// Helper Functions
endGame() {
this.scene.stop("GameScene");
this.scene.start("EndScene");
game.input.enabled = true;
}
determineWinner() {
if (playerCoins === 3 || compCoins === 3) {
this.endGame();
}
}
} // end GameScene
Could somebody please tell me what I’m doing wrong? I cannot for the life of me understand, and I have tried almost everything - except, apparently, the correct thing