my healthbar don't go down
it keep show up the game over when touching the player
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
boolean touchinggator = false;
private GreenfootImage myImage = getImage();
private String[] imageNames = {"player_run1.png","player_run2.png","player_run3.png"};
private int currentImage = 0;
int xPos;
int yPos;
public void addedToWorld(World w)
{
xPos = getX();
yPos = getY();
}
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-10);
}
if(Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+10);
}
if(Greenfoot.isKeyDown("left"))
{
setLocation(getX()-10, getY());
}
if(Greenfoot.isKeyDown("right"))
{
setLocation(getX()+10, getY());
}
animateOnMove();
}
private void animateOnMove()
{ if(getX()!=xPos|| getY()!=yPos)
{
currentImage++;
if(currentImage >= imageNames.length)
currentImage=0;
GreenfootImage newImage = new GreenfootImage(imageNames[currentImage]);
if(getX()<xPos)
newImage.mirrorHorizontally();
setImage(newImage);
myImage = getImage();
}
xPos=getX();
yPos=getY();
Actor spider;
spider = getOneObjectAtOffset(0,0,spider.class);
if (spider !=null)
{
World world;
world = getWorld();
world.removeObject(spider);
scoreboard scoreboard = (scoreboard) (getWorld().getObjects(scoreboard.class).get(0));
scoreboard.score = scoreboard.score + 1;
}
}
public void hitgator()
{
Actor gator = getOneIntersectingObject(gator.class );
if (gator !=null)
{
World myworld = getWorld();
Sandstone sandstone = (Sandstone)myworld;
Healthbar healthbar = sandstone.getHealthbar();
if (touchinggator ==false)
{
healthbar .losehealth();
touchinggator = true;
if (healthbar.health <=0)
{
game_over1 game_over = new game_over1();
myworld.addObject(game_over,myworld.getWidth()/2, myworld.getHeight()/2);
myworld.removeObject(this);
}
}
else {
touchinggator = false;
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
boolean touchinggator = false;
private GreenfootImage myImage = getImage();
private String[] imageNames = {"player_run1.png","player_run2.png","player_run3.png"};
private int currentImage = 0;
int xPos;
int yPos;
public void addedToWorld(World w)
{
xPos = getX();
yPos = getY();
}
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-10);
}
if(Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+10);
}
if(Greenfoot.isKeyDown("left"))
{
setLocation(getX()-10, getY());
}
if(Greenfoot.isKeyDown("right"))
{
setLocation(getX()+10, getY());
}
animateOnMove();
}
private void animateOnMove()
{ if(getX()!=xPos|| getY()!=yPos)
{
currentImage++;
if(currentImage >= imageNames.length)
currentImage=0;
GreenfootImage newImage = new GreenfootImage(imageNames[currentImage]);
if(getX()<xPos)
newImage.mirrorHorizontally();
setImage(newImage);
myImage = getImage();
}
xPos=getX();
yPos=getY();
Actor spider;
spider = getOneObjectAtOffset(0,0,spider.class);
if (spider !=null)
{
World world;
world = getWorld();
world.removeObject(spider);
scoreboard scoreboard = (scoreboard) (getWorld().getObjects(scoreboard.class).get(0));
scoreboard.score = scoreboard.score + 1;
}
}
public void hitgator()
{
Actor gator = getOneIntersectingObject(gator.class );
if (gator !=null)
{
World myworld = getWorld();
Sandstone sandstone = (Sandstone)myworld;
Healthbar healthbar = sandstone.getHealthbar();
if (touchinggator ==false)
{
healthbar .losehealth();
touchinggator = true;
if (healthbar.health <=0)
{
game_over1 game_over = new game_over1();
myworld.addObject(game_over,myworld.getWidth()/2, myworld.getHeight()/2);
myworld.removeObject(this);
}
}
}
else {
touchinggator = false;
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class AnimWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Sandstone extends World
{
game_over1 game_over1 = new game_over1();
Healthbar thisHealthbar; // = new Healthbar ();
/**
* Constructor for objects of class AnimWorld.
*
*/
public Sandstone()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Player player = new Player();
addObject(player, 284, 175);
spider spider = new spider();
addObject(spider,473,234);
spider spider2 = new spider();
addObject(spider2,383,99);
spider spider3 = new spider();
addObject(spider3,250,363);
spider spider4 = new spider();
addObject(spider4,103,102);
scoreboard scoreboard = new scoreboard();
addObject(scoreboard,64,42);
gator gator = new gator();
addObject(gator,45,260);
thisHealthbar = new Healthbar();
addObject(thisHealthbar,400,20);
}
public game_over1 getGameOver1()
{
return game_over1;
}
public Healthbar getHealthbar()
{
return thisHealthbar;
}
public void hitgator()
{
Actor gator = getOneIntersectingObject(gator.class );
if (gator !=null)
{
World myworld = getWorld();
Sandstone sandstone = (Sandstone)myworld;
Healthbar healthbar = sandstone.getHealthbar();
if (touchinggator ==false)
{
healthbar .losehealth();
touchinggator = true;
if (healthbar.health <=0)
{
game_over1 game_over = new game_over1();
myworld.addObject(game_over,myworld.getWidth()/2, myworld.getHeight()/2);
myworld.removeObject(this);
}
}
}
else {
touchinggator = false;
}
}
}