简体   繁体   中英

Java: Super class won't override function

I have been working on a game for a while and I would like to have a different class for each type of Creature that there is. Right now, all of the different creatures' AI is run in a long switch and I would like a superclass to ovveride that function with that AI for that creature. I have this set up but it won't override.

Am I forgetting something?

Bunny.java:

package creature;

import org.newdawn.slick.opengl.Texture;

import creature.Creature;
import creature.CreatureType;
import data.Tile;

public class Bunny extends Creature{

    public Bunny(CreatureType type, float x, float y, float speed1) {
        super(type, x, y, speed1);

    }

    public void AI(int type) {
        System.out.println("test");
    }

}

Creature.java:

public Creature(CreatureType type, float x, float y, float speed1) {
    this.texture = drawImg(type.textureName);
    this.textureHamster = drawImg("creatures/HamsterFace");
    this.healthBackground = drawImg("health_background");
    this.healthForeground = drawImg("health_foreground");
    this.healthBorder = drawImg("health_border");
    this.startTile = startTile;
    this.x = x;
    this.y = y;
    this.intX = (int) x;
    this.intY = (int) y;
    this.width = texture.getImageWidth();
    this.height = texture.getImageHeight();
    this.speed1 = speed1;
    this.speed = speed;
    this.intspeed = speed;
    this.grid = grid;
    this.health = type.health;
    this.inithealth = type.health;
    this.hiddenHealth = health;
    this.startHealth = health;
    this.dir = false;
    this.dchosen = false;
    this.setx = 0;
    this.hurt = 0;
    this.panick = 0;
    this.deathWish = 0;
    this.pdir = -1;
    this.myX = x;
    this.myY = HEIGHT / 2;
    this.right = false;
    this.left = false;
    this.fade = 0;
    this.fir = true;
    this.aiType = type.aiType;
    this.yOffset = 0;
}

.....

public void AI(int type) {
    if(panic > 0)
        panic--;
    hurt();
    speed = speed1;
    switch(type) {

    case 1:
        if(panic > 0) {
            if(pickRandom(150, 300) < 10) {
                direction = !direction;
            }

            if(direction) {
                if(!right) {
                    x += speed;
                } else {
                    if(falling < 2)
                    gravity = 8;
                }
            } else {
                if(!left) {
                    x -= speed;
                } else {
                    if(falling < 2)
                    gravity = 8;
                }
            }

        } else {
            if(getRange(WIDTH / 2, myX) > 200) {
                directionCoolDown++;
                if(directionCoolDown > pickRandom(150, 3000)) {
                    direction = !direction;
                    directionCoolDown = 0;
                }



                if(direction) {
                    if(!right) {
                        x += speed / 3.2;
                    } else {
                        if(falling < 2)
                        gravity = 8;
                    }
                } else {
                    if(!left) {
                        x -= speed / 3.2;
                    } else {
                        if(falling < 2)
                        gravity = 8;
                    }
                }


            } else {
                if(myX < WIDTH / 2) {
                    direction = true;
                } else {
                    direction = false;
                }




            }
        }
        break;

    case 2:

        yOffset = -25;
        if(!angry) {
            pdir = 0;
            if(getRange(Player.getX(), myX) < 300) {
                hamsterFace = true;
            } else {
                hamsterFace = false;
            }

            if(!hamsterFace) {
                directionCoolDown++;
                if(directionCoolDown > pickRandom(150, 3000)) {
                    direction = !direction;
                    directionCoolDown = 0;
                }

                if(direction) {
                    if(!right) {
                        x += speed / 3.2;
                    } else {
                        if(falling < 2)
                        gravity = 8;
                    }
                } else {
                    if(!left) {
                        x -= speed / 3.2;
                    } else {
                        if(falling < 2)
                        gravity = 8;
                    }
                }
            }
        } else {
            pdir++;
            hamsterFace = false;
            if(myX < Player.getX()) {
                direction = true;
            } else {
                direction = false;
            }

            if(direction) {
                if(!right) {
                    x += speed / 1;
                } else {
                    if(falling < 2)
                    gravity = 8;
                }
            } else {
                if(!left) {
                    x -= speed / 1;
                } else {
                    if(falling < 2)
                    gravity = 8;
                }
            }

            if(getRange(myX, Player.getX()) < 5 && getRange(myY, Player.getY()) < 5) {
                hurtPlayer(-2);
                direction = !direction;
                if(direction) {
                    if(!right) {
                        x += speed * 10;
                    } else {
                        if(falling < 2)
                        gravity = 8;
                    }
                } else {
                    if(!left) {
                        x -= speed * 10;
                    } else {
                        if(falling < 2)
                        gravity = 8;
                    }
                }
            }
        }

        if(panic > 1) { 
            angry = true;
        } else { 
            if(pdir > pickRandom(1000,2000)) {
                angry = false;
            }
        }

        break;

    }
}

.....

(Both classes are in the same package)

EDIT: I fixed the typo....

you have in the Bunny class:

public void AI() {
    System.out.println("test");
}

in the Creature class:

public void AI(int type) {
    if(panic > 0)
    ....

so

void AI(int type) and void AI() are NOT the same method (check the signature and how they take different parameters!)

therefore the Bunny class is not overriding anything from the parent class

-- edit:

now that your classes have a method void AI(int type) then we can say that Bunny override the Creature AI method and everytime you call bunny.AI(f) your bunny method will be called!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM