Processingでクラスの勉強をしていて、
上下に動く円があって、マウスをクリックしたら円の動く方向が変わるというプログラムをはじめに書いて、
その後にクラスを使って書いていたのですが、実行するとPlayerクラスが初期化されていませんと出てしまいます。
以下が元のコードとクラスを使って書いたコードとエラー文なのですが、何が間違っているのかを教えてくれると助かります。
元のコード
int xPos;
int yPos;
int dia;
int vy;
void setup() {
size(600, 400);
xPos = width/4;
yPos = height/2;
dia = 10;
vy = -2;
}
void draw() {
background(255);
ellipse(xPos, yPos, dia, dia);
if (mousePressed == true) {
vy = -vy;
}
yPos = yPos + vy;
}
Mainタブ
Player player;
void setup(){
size(600,400);
player = new Player();
}
void draw(){
background(255);
Player player;
player.update();
player.draw();
}
Playerタブ
class Player {
int xPos;
int yPos;
int dia;
int vy;
Player() {
xPos = width/4;
yPos = height/2;
dia = 10;
vy = -2;
}
void update() {
if (mousePressed == true) {
vy = -vy;
}
yPos = yPos + vy;
}
void draw() {
ellipse(xPos, yPos, dia, dia);
}
}
エラー文
The local veriable player may not have been initialized
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。