I have no Idea how I would make my image switch after my enemy dies. here is my players code:and here is my enemies (AI) code:
again, I would like gohan's image to be changed to image8(on gohan's code) or "Gpowerup.wav"(on cell's code) if cells health<=0 I don't even have a clue how to start writing the code for this, so anything is helpful. You guys haven't disappointed me yet, don't let me down this time.
import greenfoot.*;
/**
* Write a description of class Gohan here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Gohan extends Actor
{
private int health = 900000; //its over 9000
public void setHealth(int points) {
health += points;
}
public int getHealth() {
return health;
}
private GreenfootImage image1;
private GreenfootImage image2;
private GreenfootImage image3;
private GreenfootImage image4;
private GreenfootImage image5;
private GreenfootImage image6;
private GreenfootImage image7;
private GreenfootImage image8;
public Gohan()
{
image1 = new GreenfootImage("gohan.png");
image2 = new GreenfootImage("gohanstep.png");
image3 = new GreenfootImage("gohanstep2.png");
image4 = new GreenfootImage("powerattack.png");
image5 = new GreenfootImage("gohanpunch.png");
image6 = new GreenfootImage("Gohankick1.png");
image7 = new GreenfootImage("HealthBar.png");
image8 = new GreenfootImage("Gpowerup.png");
setImage(image1);
}/**
* Act - do whatever the Gohan wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeypress ();
}
public void checkKeypress()
{
if (Greenfoot.isKeyDown("left"))
{
move(-30);
if ( getImage() == image1 )
{ setImage(image2);
}
else
{if ( getImage() == image8 ){}
else{ setImage(image1);
}
}
}
if (Greenfoot.isKeyDown("right"))
{ move (30);
if ( getImage() == image1 )
{ setImage(image2);
}
else setImage(image1);
}
if (Greenfoot.isKeyDown("s"))
{
setImage(image3);
hitEnemy();
}
else if ( getImage() == image3)
{
setImage(image1);}
if (Greenfoot.isKeyDown("z"))
{
setImage(image3);
hitEnemy();
}
if (Greenfoot.isKeyDown("x"))
{
setImage(image6);
hitEnemy();
}
else if ( getImage()== image6)
{setImage(image1);}
if (Greenfoot.isKeyDown("a"))
{
setImage(image5);
hitEnemy();
}
else if ( getImage()== image5)
{setImage(image1);}
if (Greenfoot.isKeyDown("space"))
{
setImage(image4);
hittingEnemy();
}
else if ( getImage() == image4 )
{setImage(image1);}
if (health<=0)
{
Greenfoot.playSound("cellyelling.wav");
getWorld().removeObject(this);
}
}
private void hitEnemy() {
Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
if (cell != null) {
cell.setHealth(-1001);
cell.move(30);
}
}
private void hittingEnemy() {
Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
if (cell != null) {
cell.setHealth(-3000);
cell.move(200);
}
}
}
}
import greenfoot.*;
/**
* Write a description of class Cell here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Cell extends Actor
{
/**
* Act - do whatever the Cell wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
//this works
public int health = 900000;
public void setHealth(int points) {
health += points;
}
public int getHealth() {
return health;
}
private GreenfootImage image1;
private GreenfootImage image2;
private GreenfootImage image3;
public Cell()
{
image1 = new GreenfootImage("Cell.png");
image2 = new GreenfootImage("Cellpunch.png");
image3 = new GreenfootImage("Cellpunch2.png");
}
public void act()
{
if (Greenfoot.isKeyDown("a"))
{
setImage(image1);
}
if (Greenfoot.isKeyDown("s"))
{
setImage(image1);
}
if (Greenfoot.isKeyDown("z"))
{
setImage(image1);
}
if ( Greenfoot.getRandomNumber(100) < 10 )
{
setImage(image1);
move(20);
}
if ( Greenfoot.getRandomNumber (100) < 40 )
{
move(-60);
}
if ( Greenfoot.getRandomNumber (100) < 90 )
{ if ( getImage() == image1 )
{ setImage(image2);
}
if (Greenfoot.isKeyDown("shift"))
{}
else
{
hitEnemy();
}
}
if ( Greenfoot.getRandomNumber (100) < 60 )
{
setImage(image3);
if (Greenfoot.isKeyDown("shift"))
{}
else
{
hitEnemy();
}
}if (health<=0)
{
}
dead();
}
private void dead() {
if (health <=0)
{
Greenfoot.playSound("gohanyelling.wav");
getWorld().removeObject(this);
}
}
private void hitEnemy() {
Gohan gohan = (Gohan) getOneObjectAtOffset(0, 0, Gohan.class);
if (gohan != null) {
gohan.setHealth(-3000);
gohan.move(-100);
}
}
}
