[英]I'm trying to create random object using arrays and class with random locations?
我正在嘗試使用類創建一個游戲,其中對象移動到隨機位置並使用數組以隨機數量相加。 有人可以幫我更好地編寫代碼,因為它不起作用嗎? 順便說一下,我正在使用“處理”軟件。
我的課
*final color ALIEN_COLOR = color(30, 100, 0);
PImage background;
int x=0; //global variable background location
Superhero hero1;
Alien [] invader1 = new Alien[8];
void setup(){
size(800,400);
background = loadImage("spaceB.jpg");
background.resize(width,height);
hero1 = new Superhero(10, height/2);
for(int i = 0; i < invader1.length; i++){
invader1[i] = new Alien();
invader1 = new Alien(width,300);
}
} // setup ends
void draw ()
{
drawBackground();
hero1.render();
invader1.render();
if(invader1.move() == false){
invader1 = new Alien(width, 500);
}
} // draw ends*
對象為:
***class Alien{
int x;
int y;
Alien(int x, int y){
this.x = x;
this.y = y;
}
void render(){
fill(ALIEN_COLOR);
rect(x, y, 50, 50);
}
boolean move(){
x = x - 1;
return (x >= 0);
}
}***
我收到的錯誤消息是:
你打電話給invader1[i] = new Alien();
但是您在Alien
類中沒有無參數構造函數。 在Alien
類中聲明一個無參數構造函數,如下所示,以解決這個問題:
Alien() {
// Put here some initialization code if needed else leave it as it is
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.