我連接到遠程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點是從服務器下載的唯一東西。
在客戶端使用Javascript或Flash進行動畫 – 2010-11-05 15:20:51
也許配置您的服務器以較小的塊發送? – Dereleased 2010-11-05 15:26:15