Interface method and subclass does not exist!

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

  • Answer ✓

    Where does the animal class finish?

  • It finished after the display(); method. Thanks.

  • edited March 2018

    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

    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();
    }
    

    class tab

    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;
      }
       void display() {
        imageMode(CENTER);
        image(anImal, posX, posY, 150, 150);
      }
      }
    
      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;
          }
        }
      }
    
  • 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.

    Furry Dog= new Furry("dog", "dog.png",100,"Brown");
    Wild Cat= new Wild("cat","cat1.png",100);
    

    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!!

  • edited March 2018 Answer ✓

    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

    Furry dog;
    Wild cat;
    
    void setup(){
    size(1600,800);
    
     dog= new Furry("dog", "dog.png",100,"Brown");
     cat= new Wild("cat","cat1.png",100);
    
    
    }
    
    void draw(){
      background(255,255);
      cat.display();
      dog.display();
    
      cat.jumpAt();
    }
    

    The class

        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;
          }
           void display() {
            imageMode(CENTER);
            image(anImal, posX, posY, 150, 150);
          }
          }
    
          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;
    
              }
            }
          }
    
  • Answer ✓

    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.

Sign In or Register to comment.