2
//----------------------------------------------\\
float x = 300;
float y = 300;
float direction = 0;
float increment = 1;
float speed = 5;
boolean toggle = true; // - For spaceship reversal
float wormX = random(0, 600); // - For wormHole v
float wormY = random(0, 600);
float wormGrowth = 0;
boolean growthSwitch = true; // - for wormHole^
float[] starXpos = new float[100]; //starsRandom
float[] starYpos = new float[100]; //starsRandom
float d = dist(x, y, wormX, wormY);
int score = 0;
//----------------------------------------------\\
//----------------------------------------------\\ Setup
void setup(){
size (600, 600);
starsP1();
}
//----------------------------------------------\\ Draw
void draw(){
background (0);
spaceShip();
starsP2();
wormHole();
score();
warpInitial();
blackHoleAt(100, 40);
blackHoleAt(400, 500);
}
//----------------------------------------------\\
//----------------------------------------------\\ starsRandom
void starsP1(){
int i = 0;
while (i < 100){
starXpos[i] = random(0, width);
starYpos[i] = random(0, height);
i = i + 1;
}
}
void starsP2(){
stroke(255);
strokeWeight(5);
int i = 0;
while (i < 100){
point(starXpos[i], starYpos[i]);
i = i + 1;
}
if (key == 'w'){
starYpos[i] = starYpos[i] + 1;
}
}
我試圖在代碼中爲星星創建一種視差形式。當用戶按下w,a,s,d時,恆星陣列應該與方向一致。我不明白這應該如何工作,因爲我不斷收到此錯誤。數組索引超出範圍例外:100
在'starsP2'中的while循環之後,您的'i'變量的值爲'100',而最大可能的數組元素爲'99'。當你創建'new float [100]'數組時,它具有從'0'到'99'的元素。 –
我該如何改變這種情況,以便當'w'被按下時恆星移動? – Christian452