I am having an issue where i want the class Wombat.class to use the method getHealthTwo(), from Wombat2.class, and vise versa with Wombat2.class having a constructor to use getHealth() from Wombat.class. The problem is that if i use the constructor on only one, it works fine having no problems at all, but i need two for my thing to work, and a constructor for each on each health doesn't work and throws me an error saying that there is an error on that line of code even though when i compile it it compiles fine.
Heres the code for Wombat.class:
Heres the code for Wombat2.class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Wombat here. * * @author (your name) * @version (a version number or a date) */ public class Wombat extends Actor { /** * Act - do whatever the Wombat wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private Wombat2 wbTwo = new Wombat2(); private int cooldown = 0; public int health = 10; private boolean jumping; private int jumpStrength = 10; private int vSpeed = 0; private int acceleration = 1; public void act() { if(health != 1){ controls(); checkFall(); hit(); push(); if(cooldown > 0) { cooldown -= 1; } } } private void controls() { if (Greenfoot.isKeyDown("a")){ move(-3); } if (Greenfoot.isKeyDown("d")){ move(3); } if(Greenfoot.isKeyDown("w") && jumping == false){ jump(); } if(Greenfoot.isKeyDown("space") && (cooldown == 0)) { if(wbTwo.getHealthTwo() >= 3){ cooldown = 40; Greenfoot.playSound("LASER.wav"); shoot(); } else{ cooldown = 0; Greenfoot.playSound("LASER.wav"); shoot(); } } } private void fall() { setLocation(getX(), getY() + vSpeed); if(vSpeed <= 9) { vSpeed = vSpeed + acceleration; } jumping = true; } private boolean onGround() { int wombatHeight = getImage().getHeight(); int yDistance = (int) (wombatHeight / 2 + 5); Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Floor.class); if(ground == null) { jumping = true; return false; } else { moveToGround(ground); return true; } } private void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jumping = false; } private void checkFall() { if(onGround()){ vSpeed = 0; } else{ fall(); } } private void jump() { vSpeed = vSpeed - jumpStrength; jumping = true; fall(); } public void hit() { if(isTouching(Lazer2.class)){ Actor actor = getOneIntersectingObject(Lazer2.class); getWorld().removeObject(actor); health = health - 1; } } public void push() { if(isTouching(Wombat2.class)){ move(-3); } } public synchronized int getHealth() { return health; } public void shoot() { int x = getX(); int y = getY(); getWorld().addObject(new Lazer(), x + 30, y); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Wombat2 here. * * @author (your name) * @version (a version number or a date) */ public class Wombat2 extends Actor { /** * Act - do whatever the Wombat2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private Wombat wbOne = new Wombat(); private int cooldown = 0; public int healthTwo = 10; private boolean jumping; private int jumpStrength = 10; private int vSpeed = 0; private int acceleration = 1; public void act() { if(healthTwo != 1){ controls(); checkFall(); hit(); push(); if(cooldown > 0) { cooldown -= 1; } } } private void controls() { if (Greenfoot.isKeyDown("left")){ move(-3); } if (Greenfoot.isKeyDown("right")){ move(3); } if(Greenfoot.isKeyDown("up") && jumping == false){ jump(); } if(Greenfoot.isKeyDown("p") && (cooldown == 0)) { if(wbOne.getHealth() >= 3){ cooldown = 40; Greenfoot.playSound("LASER.wav"); shoot(); } else{ cooldown = 0; Greenfoot.playSound("LASER.wav"); shoot(); } } } private void fall() { setLocation(getX(), getY() + vSpeed); if(vSpeed <= 9) { vSpeed = vSpeed + acceleration; } jumping = true; } private boolean onGround() { int wombatHeight = getImage().getHeight(); int yDistance = (int) (wombatHeight / 2 + 5); Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Floor.class); if(ground == null) { jumping = true; return false; } else { moveToGround(ground); return true; } } private void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jumping = false; } private void checkFall() { if(onGround()){ vSpeed = 0; } else{ fall(); } } private void jump() { vSpeed = vSpeed - jumpStrength; jumping = true; fall(); } public void hit() { if(isTouching(Lazer.class)){ Actor actor = getOneIntersectingObject(Lazer.class); getWorld().removeObject(actor); healthTwo = healthTwo - 1; } } public void push() { if(isTouching(Wombat.class)){ move(3); } } public synchronized int getHealthTwo() { return healthTwo; } public void shoot() { int x = getX(); int y = getY(); getWorld().addObject(new Lazer2(), x - 30, y); } }