Jump to content

How to hold onDown with listeners?


SoundChaos
 Share

Recommended Posts

I'm quite new to phaser, and also new to the node.js and socket.io I'm working with. I've been using a tutorial here to get started.

Making a multiplayer browser test game and I can't figure out how to hold an arrow key to have a player move. This code is working per press from inside my game.js file, but it seems like I made it overly complicated.. And I need it to be able to move players while an arrow key is held down continuously. 

Here is how I am reading the down arrow key:

var cursors = game.input.keyboard.createCursorKeys();
cursors.down.onDown.add(Client.sendKeyDown, this);

which calls my client.js as such:

Client.sendKeyDown = function(){
  Client.socket.emit('keyboardDown');
};

Which tells my server.js:

socket.on('keyboardDown',function(){
            console.log('arrow down');
            socket.player.y += 4;
            io.emit('move',socket.player);
        });

And then back to client.js:

Client.socket.on('move',function(data){
        Game.movePlayer(data.id,data.x,data.y);
    });

and then finally to game.js:

Game.movePlayer = function(id,x,y){
    var player = Game.playerMap[id];
    var distance = Phaser.Math.distance(player.x,player.y,x,y);
    var tween = game.add.tween(player);
    var duration = distance*10;
    tween.to({x:x,y:y}, duration);
    tween.start();
};

From what I can see from searching for options, is that I should be using the "isDown" method, but I can't seem to get isDown to trigger my "Client.sendKeyDown".

 

server.js

client.js

game.js

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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