node.js - p5.j​​s 和 node.js 为小 Blob 同步 x 和 y 的位置

标签 node.js p5.js

我目前正在为我的学校项目制作一个类似 agar.io 的程序,使用 p5.js 和 node.js 进行网络连接。但是,我在多人游戏模式的一个位置设置所有小 Blob 时遇到问题,因为我编写了在本地 javascript(circle.js) 上设置小 Blob 的程序。我试图将本地javascript的功能转移到server.js(node.js),但是当我调用它时,它只是挂断了。这是目录的截图。 enter image description here

这是 server.js 的代码

var express = require('express');
var app = express();
var server = app.listen(3000);

app.use(express.static('public'));

console.log("Running");

var socket = require('socket.io');

var io = socket(server);

io.sockets.on('connection', newConnection);

function newConnection(socket){
	console.log('new connection' + socket.id);


}

function asd(){
	fill(255);
    ellipse(200, 200, 100 * 2, 100 * 2);
}


这是 index.html 的代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>agar.io</title>
  <script src="libraries/p5.js" type="text/javascript"></script>
  <script src="libraries/p5.dom.js" type="text/javascript"></script>
  <script src="libraries/p5.sound.js" type="text/javascript"></script>
  <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>

  <script src="sketch.js" type="text/javascript"></script>
  <script src="circle.js" type="text/javascript"></script>
  <script src="C:/Users/hp/Desktop/p5.js/Project/agario/server.js" type="text/javascript"></script>

  <style> body {padding: 0; margin: 0;} canvas {vertical-align: top;} </style>
</head>
<body>
</body>
</html>


这是 Circle.js 的代码

function Circle(positionX, positionY, radius) {
  this.position = createVector(positionX, positionY);
  this.radius = radius;
  this.velocity = createVector(0, 0);

  this.show = function() {
    fill(255);
    ellipse(this.position.x, this.position.y, this.radius * 2, this.radius * 2);
  }

  this.update = function() {
    var newVelocity;
    velocity = createVector(mouseX - width / 2, mouseY - height / 2);
    newVelocity = createVector(mouseX - width / 2, mouseY - height / 2);
    newVelocity.setMag(3);
    this.velocity.lerp(newVelocity, 0.2);
    this.position.add(this.velocity);
    
  }

  this.eat = function(other) {
    var distance = p5.Vector.dist(this.position, other.position);
    if (distance < this.radius + other.radius) {
      var area = Math.PI * Math.pow(this.radius, 2) + Math.PI * Math.pow(other.radius, 2);
      this.radius = Math.sqrt(area / Math.PI);
      return true;
    } else {
      return false;
    }
  }

}


这是sketch.js的代码

var circle;
var circles = [];
var zoom = 1;
var newZoom;
var socket;

function setup() {
	socket = io.connect('http://localhost:3000');
  createCanvas(1366, 666);
  circle = new Circle(0, 0, 64);
  for (var x = 0; x < 410; x++) {
    circles[x] = new Circle(random(-width, width * 4), random(-height, height * 4), 20);
  }
}

function draw() {
  background(60);
  translate(width / 2, height / 2);
  newZoom = (64 / circle.radius*1.5);
  zoom = lerp(zoom, newZoom, 0.1);
  scale(zoom);
  translate(-circle.position.x, -circle.position.y);

  for (var x = circles.length - 1; x >= 0; x--) {
    if (circle.eat(circles[x])) {
      circles.splice(x, 1);
    }
  }

  circle.show();
  circle.update();


  for (var x = 0; x < circles.length; x++) {
    circles[x].show();
  }
  asd();
}


正如你所看到的,我试图在 node.js 上调用一个函数只是为了尝试从 server.js 获取信息是否有效以获得类似的小 Blob 计数和位置,我的问题是我如何制作服务器这给出了小 Blob 的 x 和 y 位置?

最佳答案

    socket.on('mouse',
      function(data) {
        // Data comes in as whatever was sent, including objects
        console.log("Received: 'mouse' " + data.x + " " + data.y);

        // Send it to all other clients
        socket.broadcast.emit('mouse', data);

        // This is a way to send to everyone including sender
        // io.sockets.emit('message', "this goes to everyone");

      }
    );

关于node.js - p5.j​​s 和 node.js 为小 Blob 同步 x 和 y 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42203409/

相关文章:

javascript - 延迟对象在解析之前返回

javascript - p5.j​​s 形状循环错误(全部卡在一点)

javascript - p5.speech.js 连续不会变成真的

javascript - p5.j​​s 粒子在 2D 矢量场影响下的行为表现出较差的响应

node.js - 使用 Nodejs 捕获事件 firebird 不起作用

javascript - 使用 js-xlsx 导出 .xlsx 文件时如何设置单元格宽度

javascript - 解决同步函数内的异步函数

javascript - 是否可以在 ml5.js 中隐藏视频但保留手部姿势点?

javascript - 当一次按下 2 个以上的键时,p5.js keyIsDown() 行为不一致

node.js - 如何使用React指令主页上的代码