处理错误消息

huangapple go评论64阅读模式
英文:

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 中,变量 keyPressedkey 没有被声明,并且大括号没有匹配。下面我已经修复了大括号,但没有修复变量声明(假设您没有显示定义它们的代码):

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 variable k is declared somewhere with a line like karaktar k;.
  • background(#F5CD0C); the method background must exist, and it looks like you are trying to pass in a hexadecimal number. In Java, hex numbers are identified by 0x, not #. I imagine that line should be background(0xF5CD0C);
  • size(800, 800); The method size also has to exist.
  • strokeWeight(20); ditto, must exist somewhere.
  • frameRate(24); ditto
  • if (lastKey != key) { variable key must be declared somewhere, presumably an int to compare to lastKey.
  • k.move(); again, k must 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;
      }
    }
  }
}

huangapple
  • 本文由 发表于 2020年5月31日 00:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/62105534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定