Java 处理错误:函数init()不存在
主类:Java 处理错误:函数init()不存在,java,Java,主类: Player thePlayer = new Player(); Guard theGuard = new Guard(); void setup() { size(1000, 500); theGuard.init(); thePlayer.init(); } void updateGame() { thePlayer.update(); } void drawGame() { background(0); thePlayer.draw(); fil
Player thePlayer = new Player();
Guard theGuard = new Guard();
void setup() {
size(1000, 500);
theGuard.init();
thePlayer.init();
}
void updateGame() {
thePlayer.update();
}
void drawGame() {
background(0);
thePlayer.draw();
fill(color(255, 255, 255));
text("Score:" + score, 10, 20);
}
void draw() {
fill (255, 255, 255);
rect(0, 401, 1000, 100);
noFill();
updateGame();
drawGame();
}
防护等级
class Guard {
float x, y;
float vx, vy;
float fillColor;
float diameter;
}
void init() {
}
void update() {
if (x == (irandom(width)-100))
vx = 3;
if (x == (irandom(width)+100))
vx = -3;
x += vx;
y += vy;
}
void draw() {
fill(fillColor);
rect(x, y, diameter, diameter);
}
class Player {
float x, y;
float vx, vy;
float fillColor;
float playerHeight, playerWidth;
float jumpTime;
boolean isJumping;
}
void init() {
playerHeight = 20;
playerWidth = 20;
fillColor = color(255, 255, 255);
jumpTime = 200;
isJumping = false;
x = 100;
y = 400;
vx = 0;
vy = 0;
}
void update() {
if (keyPressed) {
if (keyCode == LEFT) vx = -2;
if (keyCode == RIGHT) vx = 2;
if (keyCode == UP) isJumping = true;
if (keyCode == DOWN) playerHeight = 10;
}
else {
vx = 0;
vy = 0;
}
if (isJumping == true) {
vy = -2;
}
else {
vy -= 0.1;
}
if (y == 100)
vy = 0;
x += vx;
y += vy;
}
void draw() {
fill(fillColor);
ellipse(x, y, playerWidth, playerHeight);
}
玩家等级
class Guard {
float x, y;
float vx, vy;
float fillColor;
float diameter;
}
void init() {
}
void update() {
if (x == (irandom(width)-100))
vx = 3;
if (x == (irandom(width)+100))
vx = -3;
x += vx;
y += vy;
}
void draw() {
fill(fillColor);
rect(x, y, diameter, diameter);
}
class Player {
float x, y;
float vx, vy;
float fillColor;
float playerHeight, playerWidth;
float jumpTime;
boolean isJumping;
}
void init() {
playerHeight = 20;
playerWidth = 20;
fillColor = color(255, 255, 255);
jumpTime = 200;
isJumping = false;
x = 100;
y = 400;
vx = 0;
vy = 0;
}
void update() {
if (keyPressed) {
if (keyCode == LEFT) vx = -2;
if (keyCode == RIGHT) vx = 2;
if (keyCode == UP) isJumping = true;
if (keyCode == DOWN) playerHeight = 10;
}
else {
vx = 0;
vy = 0;
}
if (isJumping == true) {
vy = -2;
}
else {
vy -= 0.1;
}
if (y == 100)
vy = 0;
x += vx;
y += vy;
}
void draw() {
fill(fillColor);
ellipse(x, y, playerWidth, playerHeight);
}
我在Processing(JAVA)中编写了它。但我不知道为什么我的代码不起作用。当我尝试启动游戏时,它给出了一个错误:“函数init()不存在”。此函数用于主类的第6/7行。是否有方法创建此函数或其他内容?注意定义类和方法的括号在哪里
Guard
没有init()
当然应该在
Guard
中定义init
。可能应该在它自己的文件中定义Guard
,Guard.java您的所有类都在同一个包中吗?该方法可能不可见。在以后的注释中,请给出编译器特别指出的行号。pu类buddy中的方法为class Foo{void bar(){}
您的方法不属于Guard
和Player
类:)注意Java中没有函数。所有方法都必须是类的成员init
不是这里不存在的唯一方法。如果Guard在编译器方面做得那么好,它可能是一个内部类。还有一件事,Java有构造函数。