Jump to content

Urgent help required (deadline) - Every 2nd key press is not registered. Why?


kriket
 Share

Recommended Posts

Hi guys, 

Hope you're all well. Been a while as been busy with projects. 

On a deadline for this eve and nearly finished with testing and found a tiny bug. 

I cant post the entire game code as its not an open-source project but here's the relevant code.

In create Function:

  create: function() {
       this.game.input.onDown.add(this.tapStop, this);    //mobile
       this.spacebarPress = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);     //keyboard
       this.spacebarPress.onDown.add(this.tapStop, this);      

    }

In tapStop function: 

 tapStop: function(e) {

        console.log(e.keyCode);    /*never gives anything on every 2nd key press. But on 1st and 3rd it gives 32 correctly. Nothing on fourth keypress. 32 on fifth. Nothing on 6th. 32 on 7th. NO matter how long I wait between key presses.*/ 

        this.running = false;
        this.game.input.enabled = false;

        /*and after all the other code, we allow the player to play (press keystroke) again*/

         this.game.time.events.add(1000, function(){
                this.game.input.enabled = true;
                this.running = true;
                this.game.stage.backgroundColor = 0x000000;
                this.game.input.onDown.add(this.tapStop, this);
            }, this);   
   }

 

The problem is even if I wait 5 or 50 seconds after my first key press, it never registers the second key press. But it ALWAYS registers the third time the key is pressed.  Everytime. Mobile tap works great. 

 

Its very frustrating. Please advise. 

 

Phaser 2.9.2

 

 

 

 

Link to comment
Share on other sites

Hello!

Just one question: on "create" you are setting the event for space bar only, but inside "tapStop", when the event is finished, you are setting the "tapStop" to any kind of input?

Also I ran with some issues as well regarding timer after some action, and what worked for me was to define a separate Timer, and then clear all events with the timer.stop(), like so:

create (){
//...
    this.doingAction = false;    
    this.customTimer = this.game.time.create(false);
    this.jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    this.jumpButton.onDown.add(this.action, this);
//...
}
action(){
    if (!this.doingAction){
        this.doingAction = true;
        this.customTimer.stop(true); // clear all events
        this.waitingTimer = this.customTimer.add(Phaser.Timer.SECOND * 0.5, this.attack, this);
        this.customTimer.start(); // start timer again
    }
}
attack(){
    // do stuff
    // ...
    this.doingAction = false;
}

 

Link to comment
Share on other sites

Thanks for the reply mate. 

 

"Just one question: on "create" you are setting the event for space bar only, but inside "tapStop", when the event is finished, you are setting the "tapStop" to any kind of input?"

Nice spot. I changed that to 

this.spacebarPress.onDown.add(this.tapStop, this);

but the every other press issue still remains. But your spot fixes other unknown things so thanks for that. 

 

 

I am coding in your suggestion of separate Timer now and hopefully that should fix it. Will reply back soon 

 

 

Link to comment
Share on other sites

3 minutes ago, rich said:

If they can only press the key once, then you should use 'onDown.addOnce'. Then it will clear itself after being invoked and you can safely create another 'addOnce' later when your timer has expired.

Thanks for the help rich but this addOnce didnt work for me. No errors but same issue. Every second click doesnt work. Regardless of time elapsed. 

Link to comment
Share on other sites

Great, glad it worked!

I know the issue is done, but just one comment, if that's ok! I guess that you don't have to set up the "...onDown.add(..)" inside the event, since you already did it in "create" function. Since it's the function "add" instead of "addOnce" (as @rich mentioned), you wouldn't have to worry to set it up again after the first time (which would be the case if you are using addOnce).

Just pointing it out because it could lead to some unpredictable behavior further down the road :huh:

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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