Hi, firstly thank you all for helping me out . I changed my code and posted below.
But new problem came...T^T
1.
My palyerFish eats smaller enemyfish and grows size each time it eats now, but to some kind of enemyfish, Playerfish doest eat, even it is smaller than my playerfish..Maybe something wrong with my collision? Also, I set my palyer fish same as Enemyfish with random sizes..but sometime it is too random that my player fish is soooo small and cant eat any other fishes..
2.I need my playerfish dead and spawn at the bottom of the screen..how can I do that?
3. When the enemyFish is eaten I need to show eating traces for a period of time before the enemyFish is removed.
Teacher gave some hint on this:
"This means that the playerFish should stop colliding with the enemyFish, you can do this by setting a boolean in the enemyFish to false, and once false the playerFish should NOT be checking it's collision with the enemyFish.
When the enemyFish is eaten, you can set an int to equal some number of frames which the traces will remain on screen. If your enemyFish boolean is false, you can call a dead method in your enemyFish class. The method should look like this:
void dead() {
if (traceTimer > 0) {
traceTimer--; //decrement the trace timer so that it will eventually == 0
y += 2; //traces will sink to the bottom
} else {
fishList.remove(this); //remove the current enemyFish with a traceTimer < 0
spawnNewEnemyFish(); //your enemyFish respawn method
}
}
"
He says my eat method should change to this:
void eat(Fish other) {
if (fWidth > other.fWidth) {
fishScale *= 1.1;
fWidth = bSize * fishScale;
//change this next line so that the other fish alive is false
//and do the traces before removing from the arrayList
fishList.remove(other);
}
else {
//change this next line so that the alive is false
//and do the traces, call dead method from main draw loop
player.dead();
}
}
But I am still confusing
Here are my codes:
int numFishes = 8;
ArrayList<EnemyFish> fishList = new ArrayList<EnemyFish>();
PlayerFish player;
boolean keyUp, keyDown, keyLeft, keyRight;
void setup() {
size(800, 800);
player = new PlayerFish();
noStroke();
for (int i = 0; i < numFishes; i++) {
fishList.add(new EnemyFish());
}
keyUp = keyDown = keyLeft = keyRight = false;
}
void keyPressed() {
if (key == 'w') keyUp = true;
if (key == 's') keyDown = true;
if (key == 'a') keyLeft = true;
if (key == 'd') keyRight = true;
}
void keyReleased() {
if (key == 'w') keyUp = false;
if (key == 's') keyDown = false;
if (key == 'a') keyLeft = false;
if (key == 'd') keyRight = false;
}