CREATE VIDEO GAMES WITH PHASER.JS - Treasure Hunter

Project URL - https://www.codecademy.com/paths/create-video-games-with-phaser/tracks/game-dev-learn-javascript-arrays-and-loops/modules/game-dev-project-arrays-and-loops/projects/treasure-hunter

I have restarted this project many times, and no matter how much I follow the instruction, the project doesn’t seem to work as intended.

Everything past point 19 doesn’t result in a seemingly working game.

Point 19 states “After you’ve removed the comment and saved, you will be able to highlight your specific plot location, however, clicking will not do anything.” However, whenever I clicked on the game, every plot was revealed which means clicking was doing something. Bear in mind I hadn’t edited the section of code before this apart from removing the comment markers.

Furthermore, when I regardless continued following the instructions up to and including point 22, in the end when executing the code, the computer turn either never starts (if I include the code listed under the comment ‘// Wrap this in your if statement’) or, if I didn’t include that segment in the ‘if’ block and left it as it was, then the computer has half of the plots worth of turns in one go! It turns half the plots dug or as gold.

I am really stumped as I have tried following the instruction 4 or 5 times now and I can’t get anything past point 19 working. Does the issue lie in my lack of understanding, or the pre-existing code?

Below is the code as I have edited it - original code can be found through URL link at top.

       // 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() {
              for (let i = 0; i < coinCollection.length; i ++) {
                if (clickedPlot === coinCollection[i]) {
                  playerCoins ++;
                  clickedPlot.setTexture('playerGold');
                  setTimeout(function () {
                  compTurn()
                }, 450);  
                  break;
                }
              }
            }
            playerCheck();
          } else {
            function input() {
              game.input.enabled = true;
            }
            setTimeout(function () {
              input()
            }, 1000)
          }
        })

I don’t have access to the instructions, so I don’t know what Steps 19-22 specify.

But just trying to follow the logic of the snippet you posted:

  • if the tile clicked by you is 'blank', then you set game.input.enabled to false and execute the playerCheck function. In the playerCheck function, you are using a for loop to check whether the clickedPlot is in coinCollection. If so, you change the texture of the tile and then give the computer a turn.

My question is: What happens if the tile clicked by you is not in coinCollection? Do the instructions specify what you are supposed to do in that situation?

In the code posted by you, nothing happens in that situation. The for loop will finish without doing anything and in such a scenario, currently:

  • The tile’s texture is not being changed to something other than 'blank'

  • The computer is not being given a turn.

  • game.input.enabled is also remaining false, so you can’t interact with the game.