Mole unearther keeps pause after pressing J, K or L

Hi coders! I have some problems when i run my Mole unearther game. When i press ‘J’, ‘K’ or ‘L’ the game will pause. here are some of the codes i’ve written. If i press one of the keys I get the message in the bugsystem that onBurrowHit is not defined.

let currentBurrowKey;

let timeLeft = 30;
let score = 0;
let isPaused = false;

const gameState = {};

Those are the global variables.

// set up scene visuals, animations, and game logic when events occur
	create() {
    const updateTimer = () => {
      timeLeft -= 1;
    }
		// executed after every second passes
		const onSecondElapsed = () => {
			if (isPaused === false) {
        updateTimer();

That is the function that should pause and unpause the game.

update() {
		if (timeLeft <= 0) {
			// Provided for user
			this.scene.stop('GameScene');
			this.scene.start('EndScene');
		}

		// user successfully hit the mole, so reward the user with 5pts
		const applyHitReward = () => {
			// display how many points the user will gain
			this.displayRewardText();

			// display the new score to the user
			this.updateScoreText();
		};

		// user missed the mole, so penalize the user by taking away 5pts
		const applyMissPenalty = () => {
			// display how many points the user will lose
			this.displayPenaltyText();
      
      // will run when any of those keys are pressed. Then, it will distribute rewards and penalties accordingly.
      const onBurrowHit = key => {
        if(key === currentBurrowKey) {
          applyHitReward();
          this.relocateMole();
          isPaused = false;
          onSecondElapse();

        } else {
          applyMissPenalty();
          isPaused = false;
          onSecondElapse();

        }
        
      }

			// display the new score to the user
			this.updateScoreText();
		};

		if (isPaused === false) {
			// check each burrow's location if the user is hitting the corresponding key
			// and run the handler to determine if user should get a reward or penalty
			if (Phaser.Input.Keyboard.JustDown(gameState.jKey)) {
        onBurrowHit();

			} else if (Phaser.Input.Keyboard.JustDown(gameState.kKey)) {
        onBurrowHit();

			} else if (Phaser.Input.Keyboard.JustDown(gameState.lKey)) {
        onBurrowHit();

			}
		}

		if (Phaser.Input.Keyboard.JustDown(gameState.spaceKey)) {

		}
	}

Thats the main funtion where i think things go wrong. I would really appreciate if someone would check my code!

My guess is the game pauses because the code encounters an error.

Some things I noticed:

  • It appears onBurrowHit expects an input, but you are not passing any.

  • currentBurrowKey is not being assigned a value

I guess both influence how onBurrowHit performs

Hope this helps,
Happy coding!

1 Like

First of all, big thanks for your fast reply!

I fixed it!

Let me show you what the problem was.

// periodically checks and handles user input by updating game logic
	update() {
		if (timeLeft <= 0) {
			// Provided for user
			this.scene.stop('GameScene');
			this.scene.start('EndScene');
		}

		// user successfully hit the mole, so reward the user with 5pts
		const applyHitReward = () => {
			// display how many points the user will gain
			this.displayRewardText();

			// display the new score to the user
			this.updateScoreText();
		};

		// user missed the mole, so penalize the user by taking away 5pts
		const applyMissPenalty = () => {
			// display how many points the user will lose
			this.displayPenaltyText();
      
     
			// display the new score to the user
			this.updateScoreText();
		};
    
    // will run when any of those keys are pressed. Then, it will distribute rewards and penalties accordingly.
     const onBurrowHit = key => {
        if(key === currentBurrowKey) {
          applyHitReward();
          this.relocateMole();

        } else {
          applyMissPenalty();
        }
        
      }

		if (isPaused === false) {
			// check each burrow's location if the user is hitting the corresponding key
			// and run the handler to determine if user should get a reward or penalty
			if (Phaser.Input.Keyboard.JustDown(gameState.jKey)) {
        onBurrowHit('j');

			} else if (Phaser.Input.Keyboard.JustDown(gameState.kKey)) {
        onBurrowHit('k');

			} else if (Phaser.Input.Keyboard.JustDown(gameState.lKey)) {
        onBurrowHit('l');

			}
		}

		if (Phaser.Input.Keyboard.JustDown(gameState.spaceKey)) {

		}
	}

I’d put the function inside another fuction called the applyMissPenaly() function. This gave me the problem when i called the function outside that box.

That was why the function was not defined when I called it.

Above shows the correct form of the code now. I’ve also passed a input in onBurrowhit and things are running like it should be now!

Thanks again for your reply!

2 Likes

No problem Nico, anytime!

You can mark your solution as the “solution” and this thread will close automatically after 18 hours.

Happy coding!

1 Like