Hi!
I am trying to make the characters regenerate health over time if they aren't touching one another, being attacked, or if they aren't at the bottom edge of the world. So far, I have the characters take damage if they make contact and remain in contact. I also tried making them regenerate health using an if-statement but it doesn't seem to work. Any ideas? Thanks in advance!
import greenfoot.*;
public class Arena1 extends World
{
private static final int GAP = 20;
GroundField groundfield = new GroundField();
Batman batman = new Batman();
Sakura sakura = new Sakura();
HealthBar healthbar = new HealthBar();
HealthBar2 healthbar2 = new HealthBar2();
ManaBar manabar = new ManaBar();
ManaBar2 manabar2 = new ManaBar2();
public Arena1()
{
super(800,601,1);
prepare();
}
public HealthBar getHealthBar()
{
return healthbar;
}
public HealthBar2 getHealthBar2()
{
return healthbar2;
}
public ManaBar getManaBar()
{
return manabar;
}
public ManaBar2 getManaBar2()
{
return manabar2;
}
private void prepare()
{
addObject(groundfield,400,400);
addObject(batman,67,362);
addObject(sakura,732,362);
addObject(healthbar,140,58);
floatingFieldMethod();
addObject(healthbar2,660,58);
}
public void floatingFieldMethod()
{
int x;
int y = 310;
while(y < 325)
{
x = 191;
while(x < 650)
{
FloatingField floatingfield = new FloatingField();
addObject(floatingfield,x,y);
x = x + 398 + GAP;
}
y = y + 13 + GAP;
}
floatingFieldMethod2();
}
public void floatingFieldMethod2()
{
FloatingField floatingfield = new FloatingField();
addObject(floatingfield,398,229);
}
}
import greenfoot.*;
import java.awt.Color;
public class Batman extends Characters
{
private int fallSpeed = 0; //vertical speed.
private int fallAcceleration = 1; //increase in vertical speed over time.
BatmanControls batmancontrols = new BatmanControls(this); //calls the BatmanControls.
private int restcounter = 0;
private GreenfootImage Batman0 = new GreenfootImage("Batman0.png");
private GreenfootImage Batman1 = new GreenfootImage("Batman1.png");
private GreenfootImage Batman2 = new GreenfootImage("Batman2.png");
private GreenfootImage Batman3 = new GreenfootImage("Batman3.png");
private GreenfootImage Batman4 = new GreenfootImage("Batman4.png");
private int frame = 1;
private int animationCounterRight = 0;
private GreenfootImage Batman5 = new GreenfootImage("Batman5.png");
private GreenfootImage Batman6 = new GreenfootImage("Batman6.png");
private GreenfootImage Batman7 = new GreenfootImage("Batman7.png");
private GreenfootImage Batman8 = new GreenfootImage("Batman8.png");
private GreenfootImage Batman9 = new GreenfootImage("Batman9.png");
private int frame2 = 1;
private int animationCounterLeft = 0;
boolean touchingSakura = false;
public void act()
{
batmancontrols.batmanArrows(3);
checkFall();
if(Greenfoot.isKeyDown("Right") && this.getWorld() != null) //sets condition to carry out the "moveRight" method.
{
moveRight();
restcounter = 0;
}
else if(Greenfoot.isKeyDown("Left") && this.getWorld() != null) //sets condition to carry out the "moveLeft" method.
{
moveLeft();
restcounter = 0;
}
if(Greenfoot.isKeyDown("Up") && this.getWorld() != null) //sets condition to carry out the "jump" method.
{
if(onGround())
{
jump();
restcounter = 0;
}
}
if(!onGround())
{
restcounter = 20;
if((restcounter == 20) && Greenfoot.isKeyDown("Right"))
{
setImage("BatmanJumpRight.png");
}
if((restcounter == 20) && Greenfoot.isKeyDown("Left"))
{
setImage("BatmanJumpLeft.png");
}
}
if((!Greenfoot.isKeyDown("Up") && !Greenfoot.isKeyDown("Left") && !Greenfoot.isKeyDown("Right")) && this.getWorld() != null)
{
restcounter++;
if(restcounter > 5)
{
setImage(Batman0);
}
}
if(getY() >= 600)
{
setImage("SkeletonRight.png");
World myWorld = getWorld();
Arena1 arena1 = (Arena1)myWorld;
HealthBar healthbar = arena1.getHealthBar(); //calls the "healthbar" class via Arena1.
healthbar.loseHealth(); //calls the "loseHealth" method from the HealthBar class.
setLocation(22,600);
}
hitSakura();
}
public void hitSakura()
{
Actor Sakura = getOneIntersectingObject(Sakura.class);
if(Sakura != null)
{
World myWorld = getWorld();
Arena1 arena1 = (Arena1)myWorld;
HealthBar healthbar = arena1.getHealthBar();
if(touchingSakura == false)
{
healthbar.loseHealth(); //calls the "loseHealth" method from the HealthBar class.
//touchingSakura = true;
if(healthbar.health <= 0)
{
arena1.removeObject(this);
}
}
else if(touchingSakura == true)
{
healthbar.regenHealth();
}
}
}
public void checkFall()
{
if(onGround())
{
fallSpeed = 0;
}
else
{
fall();
}
}
public void fall()
{
setLocation(getX(),getY() + fallSpeed); //determines where and how fast the character falls.
fallSpeed = fallSpeed + fallAcceleration; //illustrates the increase in speed as the character free falls.
}
public boolean onGround()
{
int batmanHeight = getImage().getHeight();
int groundSearch = (int)(batmanHeight/2) + 5;
Actor FloatingPlatformSearch = getOneIntersectingObject(FloatingField.class);
if(FloatingPlatformSearch == null)
{
Actor GroundFieldSearch = getOneObjectAtOffset(0,groundSearch,GroundField.class);
return GroundFieldSearch != null; //tells us if the character is touching the ground
}
else //why does this work temporarily
{
Actor FloatingFieldSearch = getOneObjectAtOffset(0,groundSearch,FloatingField.class);
return FloatingFieldSearch != null; //tells us if the character is touching the ground
}
}
public void moveRight() //slows down the animating process to make the movement visible. ONLY FOR BATMAN.
{
animationCounterRight++;
if(animationCounterRight % 4 == 0)
{
animateRight();
}
}
public void animateRight() //animation process. ONLY FOR BATMAN.
{
if(frame == 1)
{
setImage(Batman1);
}
else if(frame == 2)
{
setImage(Batman2);
}
else if(frame == 3)
{
setImage(Batman3);
}
else if(frame == 4)
{
setImage(Batman4);
frame = 1;
return;
}
frame++;
}
public void moveLeft() //slows down the animating process to make the movement visible. ONLY FOR BATMAN.
{
animationCounterLeft++;
if(animationCounterLeft % 4 == 0)
{
animateLeft();
}
}
public void animateLeft() //animation process. ONLY FOR BATMAN.
{
if(frame2 == 1)
{
setImage(Batman6);
}
else if(frame2 == 2)
{
setImage(Batman7);
}
else if(frame2 == 3)
{
setImage(Batman8);
}
else if(frame2 == 4)
{
setImage(Batman9);
frame2 = 1;
return;
}
frame2++;
}
public void jump() //allows Batman to jump, but only when he is touching the ground.
{
fallSpeed = -13;
animateJump();
fall();
}
public void animateJump()
{
Actor Batman;
Batman = getOneObjectAtOffset(0,0,Batman.class);
if(Batman == null && Greenfoot.isKeyDown("Right"))
{
setImage("BatmanJumpRight.png");
}
if(Batman == null && Greenfoot.isKeyDown("Left"))
{
setImage("BatmanJumpLeft.png");
}
if(Batman == null && Greenfoot.isKeyDown("Up"))
{
setImage("BatmanJumpRight.png");
}
}
}import greenfoot.*;
import java.awt.Color;
public class HealthBar extends HealthBars
{
int health = 200; //total hitpoints of the character
int healthBarWidth = 200; //horizontal length of the bar
int healthBarHeight = 15; //vertical length of the bar
int pixelsPerHealthPoint = (int)((healthBarWidth)/(health)); //rate of fill.
public HealthBar() //constructor
{
update();
}
public void act()
{
update();
}
public void update()
{
setImage(new GreenfootImage(healthBarWidth + 2,healthBarHeight + 2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.WHITE);
myImage.drawRect(0,0,healthBarWidth + 1,healthBarHeight + 1);
myImage.setColor(Color.RED);
myImage.fillRect(1,1,health * pixelsPerHealthPoint,healthBarHeight);
}
public void loseHealth()
{
health--;
if(getY() >= 600)
{
setImage(new GreenfootImage(healthBarWidth + 2,healthBarHeight + 2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.WHITE);
myImage.drawRect(0,0,healthBarWidth + 1,healthBarHeight + 1);
}
}
public void regenHealth()
{
if ((health > 0) && (health <= 50))
{
health++;
}
}
}
