Jump to content

Resetting game.input.onDown in the same way as Phaser.Key.reset


ylluminarious
 Share

Recommended Posts

Hi all,

 

I'm having a problem where I need to reset the game.input.onDown event in the same way as Phaser.Key.reset. As you can see in the docs, that particular method is useful for resetting a key's state. I want to do that same sort of thing with the click / tap event. Here's effectively what I'm wanting, in pseudo code:

this.game.input.onDown.add(foo);...this.game.input.onDown.reset();

Is something like this possible to do?

Link to comment
Share on other sites

I looked into this issue a bit, and found out about the Phaser.Pointer class, which appears to have what I need in its reset method. Unfortunately, it looks like there are a bunch of different potential pointer objects in the Phaser.Input class, and now 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?

Link to comment
Share on other sites

Just for reference, I created an example that mimics 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.

Link to comment
Share on other sites

I think the problem is the if statement in your jumpOrTryAgain function, not the lack of a reset method. Instead of using gameOverBool, why not transition to a different state? Or maybe the same state again? Make a "preload" state for your images, then make this one (sans preload method) the "main" state. Then maybe a "gameover" state that you transition to that will reset the input handler for you?

Link to comment
Share on other sites

@drhayes I had thought about doing something like this originally, which no doubt would solve my problem, but I just think it's easier and simpler to reset the objects, as I'm not transitioning over to a different screen in the "game over" part of my game; I'm simply adding some text and stopping some objects from moving.

Link to comment
Share on other sites

@drhayes Yes, I thought about doing that too, but I didn't want to break some conventions of my code <_<;

 

I know, I know, the problem I'm having is unnecessary.

 

I did come up with a solution to this, though by implementing a simple counter, like so: https://gist.github.com/ylluminarious/0c14293e4b41cf41d4da#file-main-js-L51-L56

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...