2014-09-22 98 views
0

我正在玩phaser和socket.io,並希望製作多人爆發遊戲。我現在正在建造應該在2個客戶端之間同步的槳。所以當你在一個客戶端移動paddle時,它也會在另一個客戶端移動,反之亦然。phaser.js + socket.io發射「isDown」

它可以工作,但服務器發送的數據太多。問題可能出現在「isDown」中,因爲按鍵時它會非常快速地發射數據。這就是服務器瘋狂發送數據的原因。當沒有任何東西被按下時,socket也會發出數據。我想通過發送「發送檢查」來解決它,但它不能正常工作,因爲即使沒有按下任何內容,也會發送數據。如何限制客戶端/服務器僅在真正需要時發送數據?

下面是代碼:

CLIENT

// 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'); 

服務器

#!/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; 
     }; 
    }); 
}); 
+0

也不能有這幫幫我?它顯示在幻燈片102上添加更新時間:http://www.slidesearchengine.com/slide/building-multiplayer-games-with-angular-node-socket-io-and-phaser-io – Ally 2014-11-18 23:21:04

回答

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; 
     }); 

    }, 
};