Jump to content

Input handling best practice


zigazak
 Share

Recommended Posts

So i've recently been building a game in Phaser, trying to keep objects as modular as possible, which lead me to this question. 

 

I have a main game state that currently handles all input events and moves / mutates objects as necessary, e.g. player, enemies, particles, etc, each being a modular object which is required using browserify. I wanted to keep all input handling / updating within this function to reduce the function calls I'm making each frame (to reduce delta times).

 

My question is, would this actually yield noticeable efficiency improvements, or should I just pass a reference to the game object to each module to handle their own input / updating. Considering objects pass by reference, this shouldn't be an issue right?

 

FOR EXAMPLES SAKE, So basically instead of 

update: function() {    game.physics.arcade.overlap(enemyBullets, tank, bulletHitPlayer, null, this);    enemiesAlive = 0;    for (var i = 0; i < enemies.length; i++)    {        if (enemies[i].alive)        {            enemiesAlive++;            game.physics.arcade.collide(tank, enemies[i].tank);            game.physics.arcade.overlap(bullets, enemies[i].tank, bulletHitEnemy, null, this);            enemies[i].update();        }    }    if (cursors.left.isDown)    {        tank.angle -= 4;    }    else if (cursors.right.isDown)    {        tank.angle += 4;    }    if (cursors.up.isDown)    {        //  The speed we'll travel at        currentSpeed = 300;    }    else    {        if (currentSpeed > 0)        {            currentSpeed -= 4;        }    }    if (currentSpeed > 0)    {        game.physics.arcade.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);    }    land.tilePosition.x = -game.camera.x;    land.tilePosition.y = -game.camera.y;    //  Position all the parts and align rotations    shadow.x = tank.x;    shadow.y = tank.y;    shadow.rotation = tank.rotation;    turret.x = tank.x;    turret.y = tank.y;    turret.rotation = game.physics.arcade.angleToPointer(turret);    if (game.input.activePointer.isDown)    {        //  Boom!        fire();    }}

I'd have something like 

update: function() {        this.tank.update(this.game);    this.turret.update(this.game, this.tank);    this.shadow.update(this.game, this.tank);    // etc etc}
Link to comment
Share on other sites

Hmm after a bit of reading, I've noticed that gameObjects can be given a reference to an input handler by setting inputEnabled to true, I guess my original question is still valid though. Would it be better to just do it "all under the same roof".

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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