I just want to reset the rocket's location after a level up to a random location but I tried to have a method call in my world to a method in my rocket class every time that the level went up but its not working, any ideas?
Any guidance is appreciated :)
Space Code:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
/**
* Space. Something for rockets to fly in.
*
* @author Michael Kolling
* @version 1.0
*/
public class Space extends World
{
private Counter scoreCounter;
private Counter levelCounter;
private int startAsteroids = 2;
public int asteroidNumber = 2;
public int round = 1;
private int startStars = 2500;
private boolean explosionFound;
HealthBar healthbar = new HealthBar();
public Space()
{
super(600, 400, 1);
GreenfootImage background = getBackground();//use this to add text, the first line before the equal sign
background.setColor(Color.BLACK);
background.fill();
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
createStars(startStars);
//addAsteroids2(level2Asteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 380);
// levelUpper = new Levelupp("Level: ");
// addObject (levelUpper, 580, 380);
levelCounter = new Counter("Level: ");
addObject(levelCounter, 540, 380);
levelCounter.add(1);
Explosion.initializeImages();
ProtonWave.initializeImages();
addObject(healthbar, 518, 30);
}
/**
* 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(int add)
{
scoreCounter.add(100);
// score += add;
}
/**
* to increase the # of asteroids after increase in levels
*/
public void nextRound(int add)
{
asteroidNumber += add;
if(asteroidNumber == 0)
{
round++;
levelCounter.add(1);
addAsteroids(round + 1);
asteroidNumber = round + 1;
List<Rocket> rockets = getObjects(Rocket.class);
for ( Rocket rocket : rockets )
{
rocket.next();
}
}
}
/**
* this is to get our healthbar info back!
*/
public HealthBar getHealthBar()
{
return healthbar;
}
/**
* if Explosion occurs then game over, if it doesn't, continue
*/
public void act()
{
if (!explosionFound && !getObjects(Explosion.class).isEmpty()) explosionFound = true;
if (explosionFound && getObjects(Explosion.class).isEmpty()) gameOver();
}
/**
* 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 class
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--;
healthbar.adjustValue(-1);
if (healthbar.health == 0)//ERROR HERE!!!!!!
{
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
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
}
// 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
/**
* 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;
}
}
/**
*
*/
public void next()
{
getWorld().removeObject(this);
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new Rocket(), x, y);
}
}
