phaser.js + socket.io在“isDown”上发出

时间:2014-09-22 15:17:03

标签: javascript node.js sockets

我正在使用phaser和socket.io并希望制作多人突破游戏。我现在正在建造桨,它应该在2个客户之间同步。因此,当您在一个客户端中移动桨时,它也会在另一个客户端中移动,反之亦然。

它有效,但服务器发送的数据过多。问题可能在" isDown"因为按下按键时它会非常快速地发出数据。这就是为什么服务器像疯了一样发送数据。当没有按下任何内容时,套接字也会发出数据。我想通过制作"发送支票"来解决这个问题。但它无法正常工作因为即使没有按下任何内容也会发送数据。如何限制客户端/服务器仅在真正需要时发送数据?

以下是代码:

客户端

// connect to the socket server
var socket = io();

var main = {
  preload: function() {
    game.load.image('paddle', 'images/paddle.png');
  },

  create: function() { 
      game.physics.startSystem(Phaser.Physics.ARCADE);

      this.cursor = game.input.keyboard.createCursorKeys();

      this.paddle = game.add.sprite(200, 400, 'paddle');

      game.physics.arcade.enable(this.paddle);

      //game.stage.disableVisibilityChange = true;

      this.paddle.body.immovable = true;
  },

  update: function() {

    var that = this;

    var pos = this.paddle.position.x;

      if (this.cursor.right.isDown){
            that.paddle.body.velocity.x = 350;
            socket.emit('paddle-mov', {send:true}, pos);        
      } 
      else if (this.cursor.left.isDown) {
            that.paddle.body.velocity.x = -350;
            socket.emit('paddle-mov', {send:true}, pos);
      }
      else {
        that.paddle.body.velocity.x = 0;
        socket.emit('paddle-mov', {send:true}, pos);            
      }

      socket.on('paddle-mov-back', function(inx){
            console.log(inx);
            that.paddle.position.x = inx;
        });

  },
};

// Initialize Phaser, and start our 'main' state 
var game = new Phaser.Game(400, 450, Phaser.AUTO, 'gameDiv');
game.state.add('main', main);
game.state.start('main');

SERVER

#!/usr/bin/env node
var debug = require('debug')('test');
var app = require('../app');
var server = app.listen(3000);
var io = require('socket.io').listen(server);

io.on('connection', function(socket){   
    socket.on('paddle-mov', function(send, inx){
        if (send.send == true) {
            socket.broadcast.emit('paddle-mov-back', inx);
            send.send = false;
        };
    });
});

1 个答案:

答案 0 :(得分:0)

添加buttonPressed标志,您应该能够减少数据

// connect to the socket server
var socket = io();
var buttonPressed = false;

  var main = {
     preload: function() {
     game.load.image('paddle', 'images/paddle.png');
  },

  create: function() { 
      game.physics.startSystem(Phaser.Physics.ARCADE);

      this.cursor = game.input.keyboard.createCursorKeys();

      this.paddle = game.add.sprite(200, 400, 'paddle');

      game.physics.arcade.enable(this.paddle);

      //game.stage.disableVisibilityChange = true;

      this.paddle.body.immovable = true;
   },

  update: function() {

    var that = this;

    var pos = this.paddle.position.x;

      if ((this.cursor.right.isDown || this.cursor.left.isDown) && buttonPressed == false){
            buttonPressed = true;
            that.paddle.body.velocity.x = 350;
            socket.emit('paddle-mov', {send:true}, pos);        
      } 
      else {
        buttonPressed = false;
        that.paddle.body.velocity.x = 0;
        socket.emit('paddle-mov', {send:true}, pos);            
      }

      socket.on('paddle-mov-back', function(inx){
            console.log(inx);
            that.paddle.position.x = inx;
        });

  },
};