Jump to content

Hotkeys implementation. Overflow assignments.


Recommended Posts


I was looking the forum for an answer, but wasn't able to find even the issue.
Maybe it's just my wrong usage of Phaser mechanics. Anyway, I'll be gratefull for any information.

The matter:
I got two separate files ("game.js" and "player.js").
While game.js used as a "state", player.js is just an attached library with some functions.

There is hotkey binding in player.js, which is called in game.js within "update" property. You know, to make an object move around.
But when the hotkey is pressed, there are from 60 to 150 identical assignments happens. It is about 100 times more on the average than is needed.

Is there a solution to make just one assignment?


Additional information:
I'll try to explain the whole structure in few words.

In "game.js" there is variable with object

ePow = {
    x: 100*(settable int),
    y: 100*(settable int),
    currentX: 0,
    currentY: 0 }

It is called within "creaton" property of game.js from player.js's  another prototype, but I think it isn't important.

In "player.js" there is prototype "keybind", which contains creation of hotkeys, listeners for hotkeys and assignment functions.

Points of interest within keybind:

1. this.(game.js's context with sprite object).velocity.x = (game.js's context).ePow.currentX;

2. function thrustLeft () {
    (game.js's context).ePow.currentX = (game.js's context).ePow.x * -1;
3. this.(hotkey).onDown.addOnce(thrustLeft);

When hotkeys is pressed console shows me, that value was assignet for 127 times, for example.


Link to comment
Share on other sites

It would be very helpful if you showed the actual code and not summaries of it. For example, you might be assigning the hotkey every update frame. Is it updating many times because the key is being held down for 127 frames? Are you using Phaser's built-in physics, or your own?

For number 3, addOnce is probably not what you want. You want it to fire thrustLeft every time the player presses the key, right?

Why are you worrying about how often the variable is set? Do you have a setter defined that does side-effects upon assignment?

Link to comment
Share on other sites

On 17.10.2016 at 5:26 PM, drhayes said:

It would be very helpful if you showed the actual code and not summaries of it.

Thank you very much for your reply.

Well, I thought that actual code might be too massive to post it on forum "as is". I'll bring some parts in that case.

There is no really difference how many seconds the key is pressed down, assignment sequence appears just once with virtually random quantity. Can't say about "assigning on every update frame", cause of poor understanding Phaser's mechanics on that stage. Hope there might be some light shining upon that matter after you'll look at the code.

I'm using Phaser's Arcade Physics.

About "number 3". I'm trying to realize smooth movement control system. The way it is shown in example doesn't look perfect, because some keys are "in priority" of another while simultaneously pressed. So I was looking the way to bring them to "last pressed in priority" system. Ehm, my solution at the moment is far from optimized status, so if there are better solution, please, drop me the link. Or clue...

About why am I worry. Well, there are few cases. Uncontrollable number of assignations cuts me out of particular game mechanics implementation as you mentions above. And makes the programm to overloading the system.

And, the code at last. If needed I can upload the whole project to GitHub. Now there are copy of crucial parts:


Project.Game = function (game){
    this.engGrade = 1;  //multiply coefficient for calling "enginePower" object

Project.Game.prototype = {
   create: function () {
       this.ship = new Project.Player(this.game, 30, 300);
       this.ePow = this.ship.engine(this.engGrade); //"enginePower" object with velocity values

   update: function () {
    this.ship.keybind(this);  //controls
Project.Player = function (game, x, y) {

    Phaser.Sprite.call (this, game, x, y, 'ship');
    this.physics = game.physics.arcade;
return this;
Project.Player.prototype.engine = function(grade) {
    var enginePower = {
        x: 100*grade //nominal velocity with coefficient
        y: 100*grade
        currentX:0   //storing the velocity value
    return (enginePower);
Project.Player.prototype.keybind = function (gamejs) {

    this.cursors = gamejs.input.keyboard.createCursorKeys();
    this.b = gamejs.ship.body; //don't ask

    this.body.velocity.setTo(0, 0);
    this.b.velocity.x = gamejs.ePow.currentX; //bounding the velocity with object's property
    this.b.velocity.y = gamejs.ePow.currentY;
function thrustLeft () {
    gamejs.ePow.currentX = gamejs.ePow.x * -1; //direction for left

this.cursors.left.onDown.addOnce(thrustLeft);    //similiar for other 3 directions

if (this.cursors.left.isUp && !this.cursors.right.isUp) { //making the sprite to react last pressed key
    gamejs.ePow.currentX = gamejs.ePow.x;

if (!this.cursors.left.isUp && this.cursors.right.isUp) {  //the same for other direction
    gamejs.ePow.currentX = gamejs.ePow.x * -1;

if (this.cursors.left.isUp && this.cursors.right.isUp) { //removing velocity if no "x-dimension" key pressed
    gamejs.ePow.currentX = 0;
Link to comment
Share on other sites

Guys, thank you very much!

You was absolutely right about wrong calling section. It really should be within create section with "add" instead of "addOnce" property.
Just few listeners is needed within update section.

Now works perfect.

P.S.: I used "console.log" and "velocity monitoring tool (debug with changed formula of velocity assignation") for tracking the number of assignations.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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