标题翻译
Processing errorsmessages
问题
以下是您提供的代码的翻译部分:
public void setup() {
k = new karaktar();
background (#F5CD0C);
size( 800, 800 );
strokeWeight( 20 );
frameRate( 24 );
}
int lastKey = 0;
int lastKey2 =0;
void keyPressed() {
if (lastKey != key) {
lastKey = key;
k.move();
}
}
boolean start = false;
//draw
void draw() {
start = true;
}
class karaktar {
float x = 400;
float y = 500;
boolean alreadypressed = false;
float xF = x;
float yF = y-100;
float xFF = 0;
float yFF = 0;
int h1 = 50;
int w1 = 50;
int hF = h1;
int wF = w1;
float wFF = 0;
float hFF = 0;
}
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
y -= 50;
yF -= 50;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
x -= 50;
xF -= 50;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
y += 50;
yF += 50;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
x += 50;
xF += 50;
}
}
}
第一个代码部分是游戏场景设置,第二个代码部分是用于移动的正方形的定义与移动逻辑。
英文翻译
I want to create a Javacode in processing with a simple square who can be moved by pressing A,W,S and D so i wrote a code to do so but I keep getting errormessages. I was wondering if i could get help to fix the errors.
public void setup() {
k = new karaktar();
background (#F5CD0C);
size( 800, 800 );
strokeWeight( 20 );
frameRate( 24 );
}
int lastKey = 0;
int lastKey2 =0;
void keyPressed() {
if (lastKey != key) {
lastKey = key;
k.move();
}
}
boolean start = false;
//draw
void draw() {
start = true;
}
class karaktar {
float x = 400;
float y = 500;
boolean alreadypressed = false;
float xF = x;
float yF = y-100;
float xFF = 0;
float yFF = 0;
int h1 = 50;
int w1 = 50;
int hF = h1;
int wF = w1;
float wFF = 0;
float hFF = 0;
}
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
y -= 50;
yF -=50;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
x -= 50;
xF -= 50;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
y += 50;
yF += 50;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
x += 50;
xF +=50;
}
}
}
The first code is meant to be the playingfield and the second is for the moving square.
答案1
得分: 1
似乎您在方法setup()中的变量k未被声明。
请使用karaktar k = new karaktar();,或者使用
karaktar k;
someMethod() {
k = new karaktar();
}
// 注意:
另外,按照惯例,类名的首字母应以大写字母开头。我建议将class karaktar重命名为class Karaktar。
英文翻译
Seems like your variable k in method setup() is not declared.
Use karaktar k = new karaktar();, or use
karaktar k;
someMethod() {
k = new karaktar();
}
// note:
also, it's a convention to name classes with uppercase first letter. I'd suggest renaming class karaktar to class Karaktar.
答案2
得分: 1
根据您发布的内容,您可能在引用某个不存在的东西:
k = new karaktar();只有在变量k在某处声明并且有类似karaktar k;的行时才能工作。background(#F5CD0C);方法background必须存在,看起来您试图传入一个十六进制数字。在Java中,十六进制数字以0x开头,而不是#。我猜想这一行应该是background(0xF5CD0C);size(800, 800);方法size也必须存在。strokeWeight(20);同样,必须在某处存在。frameRate(24);同上if (lastKey != key) {变量key必须在某处声明,可能是一个与lastKey进行比较的int。
在您的类 karaktar 中,变量 keyPressed 和 key 没有被声明,并且大括号没有匹配。下面我已经修复了大括号,但没有修复变量声明(假设您没有显示定义它们的代码):
class karaktar {
float x = 400;
float y = 500;
boolean alreadypressed = false;
float xF = x;
float yF = y - 100;
float xFF = 0;
float yFF = 0;
int h1 = 50;
int w1 = 50;
int hF = h1;
int wF = w1;
float wFF = 0;
float hFF = 0;
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
y -= 50;
yF -= 50;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
x -= 50;
xF -= 50;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
y += 50;
yF += 50;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
x += 50;
xF += 50;
}
}
}
}
希望这对您有所帮助。
英文翻译
Looking at what you've posted, you're likely referencing something somewhere that doesn't exist:
k = new karaktar();only works if the variablekis declared somewhere with a line likekaraktar k;.background(#F5CD0C);the methodbackgroundmust exist, and it looks like you are trying to pass in a hexadecimal number. In Java, hex numbers are identified by0x, not#. I imagine that line should bebackground(0xF5CD0C);size(800, 800);The methodsizealso has to exist.strokeWeight(20);ditto, must exist somewhere.frameRate(24);dittoif (lastKey != key) {variablekeymust be declared somewhere, presumably anintto compare tolastKey.k.move();again,kmust be declared somewhere.
In your class karaktar, variables keyPressed and key were not declared, and you had unbalanced curly braces. Below I have fixed the curly braces, but not the variable declarations (in case you aren't showing the code where they are defined):
class karaktar {
float x = 400;
float y = 500;
boolean alreadypressed = false;
float xF = x;
float yF = y-100;
float xFF = 0;
float yFF = 0;
int h1 = 50;
int w1 = 50;
int hF = h1;
int wF = w1;
float wFF = 0;
float hFF = 0;
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
y -= 50;
yF -=50;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
x -= 50;
xF -= 50;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
y += 50;
yF += 50;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
x += 50;
xF +=50;
}
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!

评论