我正在处理的程序存在问题。当我使用Processing GUI运行它时,它会正常工作,但由于某种原因,当我将它添加到带有processing.js的网页时,它将无法工作。我的问题是永远不会调用keyTyped()。我试过搞乱它,但唯一远程工作的是将它添加到draw():
if(keyPressed) {
<code to add key to string>
}
然而,这甚至不起作用,更不用说了。一定有更好的方法。这是我的整个计划:
String[] chat = new String[0];
boolean loggedIn = false;
String username = "";
String query = "";
int t = 0;
String timeStamp() {
String hr, mn, se;
if (hour() < 10) {
hr = "0" + hour();
} else {
hr = hour() + "";
}
if (minute() < 10) {
mn = "0" + minute();
} else {
mn = minute() + "";
}
if (second() < 10) {
se = "0" + second();
} else {
se = second() + "";
}
return "[" + hr + ":" + mn + ":" + se + " ";
}
void addLocalMessage(String msg, String file) {
if (!file.equals("")) {
String[] file_temp = append(loadStrings(file), timeStamp() + msg);
saveStrings(file, file_temp);
} else {
String[] chat_temp = append(chat, timeStamp() + msg);
chat = chat_temp;
}
}
void addGlobalMessage(String msg) {
String[] users = loadStrings("users.txt");
for (int i = 0; i < users.length; i++) {
addLocalMessage(msg, users[i].split(" ")[0] + ".txt");
}
}
void attempt(String q) {
if (!q.substring(0, min(1, q.length())).equals(".")) {
if (loggedIn) {
if (q.length() > 0) {
addGlobalMessage("CHAT] " + username + ": " + q);
} else {
addLocalMessage("ERROR] Message length must be greater than 0.", username + ".txt");
}
} else {
addLocalMessage("ERROR] Access denied.", "");
}
} else if (q.substring(0, min(6, q.length())).equals(".login")) {
if (!loggedIn) {
String[] users = loadStrings("users.txt");
String[][] UPCombos = new String[users.length][2];
for (int i = 0; i < users.length; i++) {
UPCombos[i][0] = split(users[i], " ")[0];
UPCombos[i][1] = split(users[i], " ")[1];
}
String[] withArgs = q.split(" ");
if (withArgs.length == 3) {
for (int i = 0; i < users.length; i++) {
if (UPCombos[i][0].equals(withArgs[1]) && UPCombos[i][1].equals(withArgs[2])) {
loggedIn = true;
username = withArgs[1];
addLocalMessage("SERVER] Logged in successfully as " + username, username + ".txt");
}
}
if (!loggedIn) {
addLocalMessage("ERROR] Password is invalid.", "");
}
} else {
addLocalMessage("ERROR] Arguments count is incorrect. Please enter as: .login <username> <password>.", "");
}
} else {
addLocalMessage("ERROR] You are already logged in.", username + ".txt");
}
} else {
addLocalMessage("ERROR] Not supported", "");
}
}
void setup() {
size(1200, 600);
chat = new String[0];
frameRate(10);
addLocalMessage("SERVER] Running MSG123 v0.0.9", "");
addLocalMessage("ERROR] You are not logged in. Please log in with .login <username> <password>.", "");
}
void draw() {
if(keyPressed) {
addLocalMessage("You typed something", "");
if(key == ENTER) {
attempt(query);
query = "";
} else if(key == BACKSPACE) {
query = query.substring(0, max(0, query.length() - 1));
} else {
query += key;
}
}
background(127, 127, 255);
textSize(10);
fill(0, 0, 0);
if (loggedIn) {
chat = loadStrings(username + ".txt");
}
if (chat.length < 40) {
for (int i = 0; i < chat.length; i++) {
text(chat[i], 5, i*15 + 15);
}
} else {
int pos = 0;
for (int i = chat.length - 39; i < chat.length; i++) {
text(chat[i], 5, pos*15 + 15);
pos++;
}
}
text(query, 5, 595);
}
void keyTyped() {
if(key == ENTER) {
attempt(query);
query = "";
} else if(key == BACKSPACE) {
query = query.substring(0, max(0, query.length() - 1));
} else {
query = query + key;
}
}
我希望有人可以帮助我。我没有为处理这些事情编写代码,所以我真的不知道我在做什么:P mag 我 c
答案 0 :(得分:0)
如果你想进行密钥处理,你需要非常小心(参见http://processingjs.org/articles/p5QuickStart.html#nochartype)。在这种情况下,对于回车检查,您可能希望这样做:
if (int(key) == 13) {
...
}
对退格键(或您正在测试的任何其他键)使用类似的方法。有一些技术限制阻止JS和Java做同样的事情,上面的链接将解释所有这些。
此外,Pjs 不能执行具有相同名称的变量和函数(由于JavaScript的工作原理,请参阅http://processingjs.org/articles/p5QuickStart.html#variablenamingcare),因此keyPressed
Pjs提供了事件处理函数,但不是同名的布尔值。
解决这个问题的一个简单方法就是使用自己的:
boolean flipScreen = false;
void setup() {
size(500,500);
// no need to waste cpu cycles:
noLoop();
}
void draw() {
background(flipScreen? 255 : 0);
// gets retriggered based on user events
}
void keyPressed() {
flipScreen= true;
redraw();
}
void keyReleased() {
flipScreen= false;
redraw();
}
这也有利于在实际使用它们之后命名变量,这是编程中几乎普遍的惯例。