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();
}
GUARD CLASS
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行中使用。有沒有辦法創建這個功能或什麼的?
所有的類都在同一個包中嗎?該方法可能不可見。 –
在將來的筆記中,給出編譯器具體說的行號。 – clwhisk
pu他的班級好友裏面的方法是'class Foo {void bar(){}}'你的方法不屬於'Guard'和'Player'類:) – 2013-10-23 20:30:07