Which pointer object (i.e., cursor) to use when resetting game.input.onDown

497 views Asked by At

I'm having a problem where I need to reset the game.input.onDown event in the same way as Phaser.Key.reset. It seems like this is doable with the Phaser.Pointer.reset method. Unfortunately, there seems to be a bunch of different potential pointer objects in the Phaser.Input class, and I'm not sure which one I need to use for game.input.onDown, which says that it gets "dispatched each time a pointer is pressed down". I'm just not really sure which pointer I need to use. Can anyone shed some light on this?

I guess I effectively need to do something like this:

this.game.input.whicheverPointer.reset();

EDIT:

Here is an example that mimics the the problem I'm having.

Here is the source code for it:

var game = new Phaser.Game(800, 600, Phaser.CANVAS, "game", {preload: preload, create: create, update: update});

var dude;
var block;
var spacebar;
var gameOverText;

var gameOverCounter = 0;
var gameOverBool    = false;

function preload () {
  game.load.image("block", "assets/block.png");
  game.load.image("dude", "assets/phaser-dude.png");
}

function create () {
  dude  = game.add.sprite(373, 760, "dude");
  block = game.add.sprite(0, 505, "block");

  game.physics.arcade.enable(dude);
  game.physics.arcade.enable(block);

  dude.body.collideWorldBounds  = true;
  dude.body.gravity.y           = 200;

  block.body.collideWorldBounds = true;
  block.body.immovable          = true;

  block.body.velocity.x         = 100;
  block.body.bounce.x           = 1;

  spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

  var jumpOrTryAgain = function () {
    if (gameOverBool === false) {
      dude.body.velocity.y = -250;
      // If you open up your JS / error console, you'll see that
      // this message gets printed an extra time each reset,
      // when you click. The spacebar doesn't have this problem
      // because of the `spacebar.reset()` call below.
      console.log("down");
    } else {
      dude.destroy();
      block.destroy();
      gameOverText.destroy();
      // Here, I can reset the spacebar, but I'm not sure what to do
      // for the click / touch event, which keeps getting the jumpOrTryAgain
      // callback added onto it.
      spacebar.reset();

      gameOverBool    = false;
      gameOverCounter = 0;

      create();
    }
  };

  game.input.onDown.add(jumpOrTryAgain);
  spacebar.onDown.add(jumpOrTryAgain);
}

function update () {

  function gameOver () {
    if (gameOverCounter === 0) {
      gameOverBool = true;
      dude.body.velocity.y  = 0;
      dude.body.velocity.x  = 0;
      block.body.velocity.x = 0;
      gameOverText = game.add.text(300, 200, "Game Over!", {fontSize: "16px", fill: "white"});
      gameOverCounter += 1;
    }
  }

  game.physics.arcade.collide(dude, block, gameOver);
}

As you can read in my comments above, the problem is that the click / touch event keeps getting the jumpOrTryAgain callback added onto itself as create gets recursively called when you reset the game. I need a way to reset the click / touch event, similar to spacebar.reset() as seen above.

2

There are 2 answers

0
GDP2 On BEST ANSWER

All I had to do was implement a simple counter keeping the spacebar and pointer from getting re-assigned, like so:

var game = new Phaser.Game(800, 600, Phaser.CANVAS, "game", {preload: preload, create: create, update: update});

var dude;
var block;
var spacebar;
var gameOverText;

var gameOverCounter = 0;
var gameOverBool    = false;

var downCounter = 0;

function preload () {
  game.load.image("block", "assets/block.png");
  game.load.image("dude", "assets/phaser-dude.png");
}

function create () {
  dude  = game.add.sprite(373, 760, "dude");
  block = game.add.sprite(0, 505, "block");

  game.physics.arcade.enable(dude);
  game.physics.arcade.enable(block);

  dude.body.collideWorldBounds  = true;
  dude.body.gravity.y           = 200;

  block.body.collideWorldBounds = true;
  block.body.immovable          = true;

  block.body.velocity.x         = 100;
  block.body.bounce.x           = 1;

  var jumpOrTryAgain = function () {
    if (gameOverBool === false) {
      dude.body.velocity.y = -250;

      console.log("down");
    } else {
      dude.destroy();
      block.destroy();
      gameOverText.destroy();

      gameOverBool    = false;
      gameOverCounter = 0;

      create();
    }
  };

  if (downCounter === 0) {
    spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    game.input.onDown.add(jumpOrTryAgain);
    spacebar.onDown.add(jumpOrTryAgain);
    downCounter += 1;
  }
}

function update () {

  function gameOver () {
    if (gameOverCounter === 0) {
      gameOverBool = true;
      dude.body.velocity.y  = 0;
      dude.body.velocity.x  = 0;
      block.body.velocity.x = 0;
      gameOverText = game.add.text(300, 200, "Game Over!", {fontSize: "16px", fill: "white"});
      gameOverCounter += 1;
    }
  }

  game.physics.arcade.collide(dude, block, gameOver);
}
6
Kamen Minkov On

game.input.onDown gets triggered from both mouse and touch input. If you need to reset its callback after every call, you can just use game.input.onDown.addOnce() (instead of game.input.onDown.add()) so that the callback is removed automatically after the first run.