How can I reference my rocket class so that the explosion and the removal of my rocket instance will occur when the health bar is finished?
Thanks for the help! :)
HealthBar
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
//import java.awt.Color;
/**
* Write a description of class HealthBar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HealthBar extends Actor
{
int health = 20;
int healthBarWidth = 80;
int healthBarHeight = 10;
int pixelsPerHealthPoint = healthBarWidth/health;
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public HealthBar()
{
update();
//death();
}
public void act()
{
update();
death();
}
public void update()
{
setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
getImage().setColor(Color.WHITE);
getImage().drawRect(0, 0,healthBarWidth + 1, healthBarHeight + 1);
getImage().setColor(Color.RED);
getImage().fillRect(1, 1, health * pixelsPerHealthPoint, healthBarHeight);
}
public void death()
{
if(health == 0)
{
Space space = (Space)getWorld(); //go to our world and run this method
space.addObject(new Explosion(),getX(),getY()); // rocket actor coordinates needed here!
space.removeObject(this);// refers to the rocket (but how can I refer the rocket!) running the code
space.gameOver();//exam question!!
}
}
}Space (my world)
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* Space. Something for rockets to fly in.
*
* @author Michael Kolling
* @version 1.0
*/
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 3;
private int startStars = 2500;
HealthBar healthbar = new HealthBar();
public Space()
{
super(600, 400, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
createStars(startStars);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 380);
Explosion.initializeImages();
ProtonWave.initializeImages();
addObject(healthbar, 424, 34);
}
/**
* Add a given number of stars to our world..
*/
private void createStars(int number)
{
GreenfootImage background = getBackground();
for(int i = 0; i < number; i++)
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
int r = Greenfoot.getRandomNumber(256);
background.setColor(new Color(r,r,r));
//int g = Greenfoot.getRandomNumber(256);
//int b = Greenfoot.getRandomNumber(256);
//background.setColor(new Color(r,g,b));
background.fillOval(x, y, 4 , 4); //the two two controls the size of my stars!
//we add the same r value to make it gray if we would have many then we would get rainbow!
}
}
/**
* Add a given number of asteroids to our world. Asteroids are only added into
* the left half of the world.
*/
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
/**
*
*/
public void countScore()
{
scoreCounter.add(100);
}
/**
*
*/
public HealthBar getHealthBar()
{
return healthbar;
}
/**
* This method is called when the game is over to display the final score.
*/
public void gameOver()
{
addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2); // the coordinates div
// TODO: show the score board here. Currently missing.
}
}Rocket
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A rocket that can be c ontrolled by the arrowkeys: up, left, right.
* The gun is fired by hitting the 'space' key. 'z' releases a proton wave.
*
* @author Poul Henriksen
* @author Michael Kolling
*
* @version 1.0
*/
public class Rocket extends SmoothMover
{
private static final int gunReloadTime = 5; // The minimum delay between firing the gun.
private static final int protonWaveCalm = 250; //minimum delay between activating prton wave
private int reloadDelayCount; // How long ago we fired the gun the last time.
private int reloadPowerCount; // How long ago we activated the prton wave
private GreenfootImage rocket = new GreenfootImage("rocket.png");
private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
private boolean yes = false;
/**
* Initilise this rocket.
*/
public Rocket()
{
reloadDelayCount = 5;
reloadPowerCount = 50;
addForce(new Vector(getRotation(), 0.3));
}
/**
* Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
* accelerating and shooting when the right keys are pressed.)
*/
public void act()
{
checkKeys();
reloadDelayCount++; //increases our counter for bullets
reloadPowerCount++; //increases our counter for proton waves
ignite (Greenfoot.isKeyDown("up"));
move();
checkCollision();//leave last in our act mehthod (order matters (if rocket is gone, rocket can't check keys)
}
/**
* Check whether there are any key pressed and react to them.
*/
private void checkKeys()
{
if (Greenfoot.isKeyDown("space"))
{
fire();
}
if (Greenfoot.isKeyDown("left"))
{
//get my rotatio, and then based off of that, set my rotation
//to change by 5 degrees
setRotation(getRotation()-5);
}
if (Greenfoot.isKeyDown("right"))
{
//get my rotatio, and then based off of that, set my rotation
//to change by 5 degrees
setRotation(getRotation()+5);
}
ignite(Greenfoot.isKeyDown("up")); //isKeyDown provides the
if (Greenfoot.isKeyDown("z"))
{
//to add a prton wave into our world when this key is pressed
StartProton();
}
}
/**
* changes between two rocket images, depending on if the up key is pressed
* adds force in the direction we are facing if up is pressed
*/
private void ignite (boolean boosterOn)
{
if (boosterOn)
{
setImage ("rocketWithThrust.png");
addForce(new Vector(getRotation(), 0.3));//we are asking for direction get the rotation we are already pointing in
}
else
{
setImage ("rocket.png");
}
}
/**
* Fire a bullet if the gun is ready.
*/
private void fire()
{
if (reloadDelayCount >= gunReloadTime)
{
Bullet bullet = new Bullet (getMovement().copy(), getRotation());
getWorld().addObject (bullet, getX(), getY());
bullet.move ();
reloadDelayCount = 0;
}
}
/**
* method checks to see if we have collided with an asteroid, if we have remove rocket and add explosion BOOM!
*/
private void checkCollision()
{
//Actor a = getOneIntersectingObject(Asteroid.class);
//if (a != null) // a way to simplify (a is storing the intersecting object line)
//{ World world = getWorld(); and then what follows which are the two line in my if statement below
if (getOneIntersectingObject(Asteroid.class) != null && yes == false)// as long as it doesn't equal nothing
//instead of going to world now it goes to space but space is a world so inherited methods still not affected for other objects
{ //this is casting , the use of this in the below using space instead of world
Space space = (Space)getWorld(); //go to our world and run this method
HealthBar healthbar = space.getHealthBar();
healthbar.health--;
yes = true;
//space.gameOver(); //exam question!!! Why do we get an error here? //another question! us doing casting
}
else if (getOneIntersectingObject(Asteroid.class) != null)
{
yes = false;
}
//check if colliding with asteroid
//if we are
//remove rocket
//add explosion
//game over
//another place to see casting is in Newton's project in the smoothmover method With the double values into
}
// public void done()
// {
// Space space = (Space)getWorld(); //go to our world and run this method
// space.addObject(new Explosion(),getX(),getY()); // short answer question of statements make difference remember why did it not work when this line of
// space.removeObject(this); // refers to the rocket running the code
// space.gameOver();//exam question!!
// }
/**
* Fire up a proton if the count is ready and it has already calmed down.
*/
private void StartProton()
{
if (reloadPowerCount >= protonWaveCalm)
{
ProtonWave protonWave = new ProtonWave ();
getWorld().addObject (protonWave, getX(), getY());
reloadPowerCount = 0;
}
}
}

