2

我正在连接到远程 Web 服务器以获取存储在数据库中的鼠标移动。自从将代码放在服务器上以来,我编写的用于动画这些动作的处理程序一直非常不稳定。我意识到这是因为它必须获取信息,而不是在本地运行,但是有可能加快速度吗?这是我正在使用的代码

String get_users = "http://example.com/get_users.php";
String get_data = "http://example.com/get_data.php?user=";
ArrayList arrows;
PImage mouse;
int[] user_ids;
int num_users;

void setup() {
  size(1024, 768);
  frameRate(24);
  smooth();
  noStroke();
  mouse = loadImage("arrow-clear.png");
  arrows = new ArrayList();
  getUsers();

  for (int i = 0; i < num_users; i++){
    arrows.add(new Arrow(user_ids[i], i*400, 2*i*100));
  }
}

void getUsers(){
  user_ids = int(loadStrings(get_users));
  num_users = user_ids.length;
  println(num_users);
}

void draw() {
  background(0);

  if (frameCount % 600 == 0){
    getUsers();
    for (int i = 0; i < num_users; i++){
      arrows.add(new Arrow(user_ids[i], i*400, 2*i*100));
    }
  }

  for (int i = arrows.size()-1; i >= 0; i--) { 
    Arrow arrow = (Arrow) arrows.get(i);
    arrow.move();
    if (arrow.finished()) {
      arrows.remove(i);
    }
  }

}

class Arrow {
  String[] all_moves, move_pairs, new_moves;
  int[] moves;
  float x;
  float y;
  int id;
  int i = 0;
  Boolean is_done = false;

  Arrow(int tempID, float tempX, float tempY) {
    all_moves = loadStrings(get_data + tempID);
    id = tempID;
    x = tempX;
    y = tempY;
    if  (all_moves.length > 0){
      move_pairs = shorten(split(all_moves[0], "|"));
    }
  }

  void move() {
    if (move_pairs != null){
      if (i < move_pairs.length){
        moves = int(split(move_pairs[i], ","));
        image(mouse, moves[0], moves[1]);
        ++i;
      } else {
        all_moves = loadStrings(get_data + id);
        if  (all_moves.length > 0){
          new_moves = shorten(split(all_moves[0], "|"));
          for (int j = 0; j < new_moves.length; j++){          
            move_pairs = append(move_pairs, new_moves[j]);
          }
          println(move_pairs);
        } else {
          is_done = true;
        }
      }
    } else {
        is_done = true;
    }
  }

  boolean finished() {
    if (is_done) {
      return true;
    } else {
      return false;
    }
  }
}

编辑:澄清:处理所有动画的应用程序在本地运行。鼠标的 X 和 Y 点是唯一从服务器下载的东西。

4

2 回答 2

1

您希望将所有的运动数据(或其中的大块数据)传递给客户端,并让客户端完成为所有内容设置动画的工作。

于 2010-11-05T15:28:33.083 回答
0

我怀疑在每一帧上下载运动数据是个好主意。如果您不需要如此详细的响应,请定期从服务器获取一批动作并将它们排队等待绘制方法。否则,请确保服务器仅发送所需的数据。我意识到您只使用从服务器获取的数据的第一行 - all_moves[0]. 如果确实只有一行 - 很好。

您应该考虑使用createInput(URL)该流并从该流中读取,这样您就不需要为每个请求的移动打开一个新的输入流,但您的服务器端代码必须能够维护流并连续写入它们。

于 2011-01-12T11:26:44.137 回答