We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I always got problems with interfaces and subclass. Show as below, which shows that "the subclass does not exist" or "the function does not exist". I have no idea why this happened.Hope someone can help me. Thank you guys so much!
Here is the main tab
Animal Cat,Dog;
void setup(){
size(1600,800);
Furry Dog= new Furry("dog", "dog.png",100,"Brown");
Wild Cat= new Wild("cat","cat1.png",100);
}
void draw(){
Cat.display();
Dog.display();
Cat.jumpAt();
}
Here is my classes
interface jumpable {
void jumpAt();
}
class Animal {
String name;
int healthLevel;
PImage anImal;
int posX, posY;
Animal(String NA, String Imagefile, int health) {
NA= name;
anImal= loadImage(Imagefile);
posX=(int)random(200, 1400);
posY=(int)random(600);
health=healthLevel;
}
class Furry extends Animal {
String fur_color;
Furry (String NA, String Imagefile, int health, String fur) {
super(NA, Imagefile, health);
fur_color=fur;
}
}
class Wild extends Animal implements jumpable {
Wild (String NA, String Imagefile, int health ) {
super(NA, Imagefile, 100);
}
public void jumpAt() {
if ((keyPressed)&&(keyCode== UP )) {
Cat.posX=Dog.posX;
Cat.posY=Dog.posY;
}
}
}
void display() {
imageMode(CENTER);
image(anImal, posX, posY, 150, 150);
}
}
Answers
Where does the animal class finish?
It finished after the display(); method. Thanks.
Thank you a lot for the help Koogs! I see! I fixed it. But the method jumpAt()still doesn't work.......... Could you please tell me where I got wrong now? Sorry, I am new to processing.
Main tab
class tab
Actually you can have classes inside classes, but you need to use their full names, Animal.Furry, for instance. But that is usually not what you want.
Variables usually start with a lower case letter, so these should be called dog and cat.
That jump code looks ok, but I haven't run it myself. Add a println for debugging purposes.
I' try to use println for debugging, Thank you very much!!
Ok, I've seen it now... Line 1 defines dog and cat as Animal.
Lines 6 and 7 redefines them. That's wrong.
Define them outside setup, which makes them global variables. Instantiate them inside setup. (Just remove the first word from each of lines 6 and 7). You will have also have to be more specific on line 1, they can't be Animals now.
You are so patient. Thank you very much. I can continue to finish my program now. Have a nice day!
Here is my code which can successful loop through. For people who got the similar problem with me.
Main tab
The class
Ctrl-t in the editor will indent your code nicely (currently it goes a bit wrong after line 22). This way you can see the classes and methods clearer.