简体   繁体   English

Node.JS实时图形

[英]Real time graphics with Node.JS

I am new to Node JS and Socket.IO and almost new to JS... I am trying to make, just to learn, a mini multiplayer game. 我是Node JS和Socket.IO的新手,而JS几乎是新手...我试图制作一个迷你多人游戏,只是为了学习。 I make all the processing of the position of the players in the server and I pass all the positions to each client so that each one makes all the graphs in the browser. 我对服务器中播放器的位置进行所有处理,并将所有位置传递给每个客户端,以便每个客户端在浏览器中绘制所有图形。

In the server side I have these: 在服务器端,我有这些:

 function Player(x,y){ some things... this.graph = ''; } setInterval(function(){ for (i in players){ players_position(players[i]); } io.sockets.emit('new_data', players); }, 100); 

(with this I send every 100 ms all the players position) (与此同时,我每100毫秒发送一次所有玩家位置)

Then in the client side: 然后在客户端:

 var r = Raphael('field', 800, 600) for (i in players){ players[i].graph = r.circle(players[i].x,players[i].y).attr({fill:'blue'}); players[i].graph = players[i].graph.attr({cx : players[i].x , cy:players[i].y}); 

(this makes a graph of each player at the beginning and it works fine) (这会在开始时绘制每个玩家的图表,并且效果很好)

  socket.on('new_data', function(players){ for (i in players){ players[i].graph = players[i].graph.attr({cx : players[i].x , cy:players[i].y}); } 

MY PROBLEM is that here it says that this has no method ATTR. 我的问题是在这里说这没有方法ATTR。 As if the object.graph was not actually what I want it to be. 好像object.graph实际上不是我想要的那样。 I hope I have made myself clear enough. 我希望我已经足够清楚了。 If not, please tell me. 如果没有,请告诉我。

Thank you very much. 非常感谢你。

your question is a little bit unclear, but i think you are trying to pass the whole raphael object over socket.io ? 您的问题尚不清楚,但是我认为您正在尝试将整个raphael对象传递给socket.io?

socket.on('new_data', function(players){
   // here players is just a plain object without any methods
}

just make a console.log(players) in your browser and you will see whats going on. 只需在浏览器中创建console.log(players),您就会看到发生了什么。 a few lines of code i hope this will give you an idea. 几行代码,希望对您有所帮助。

var local_players = [] // < store your clientside players and the raphael objects

socket.on('new_data', function(playerdata){
//for every player data recieved update your local players data..
  for(var i in playerdata)
    local_players[i].attr({cx : playerdata[i].x , playerdata[i].y});
}

you will also have to make sure that every player gets his player data, so give every player an unique id, if the id from the playerdata is the same update the player position. 您还必须确保每个玩家都获取其玩家数据,因此,如果玩家数据中的ID相同,则为每个玩家提供唯一的ID,以更新玩家位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM