Hi, I have a few questions relating to issues im having with my current asteroid/spaceship game:
1) I managed to get the asteroid to disappear after being hit by 4 bullets but then when i tried to also make the bullets disappear on impact of the asteroids, the asteroids stopped disappearing .... could anyone tell me why?
2)I have Asteroids being generated randomly but there are far too many and too often ... how might i change this?
I'll attach my code for bullets and asteroids below and if anyone could help that would be great!
import greenfoot.*;
/**
* Write a description of class Asteriod here.
* @author (your name)
* @version (a version number or a date)
*/
public class Asteriod extends Actor
{
private int damage = 0;
/**
* Act - do whatever the Asteriod wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
**/
public void act()
{
destroyShip();
takeDamage();
if (getWorld() == null) return; //ensure that 'removeAtEdge' will not be executed if the object is not, at this point', in the world//
removeAtEdge();
if (getWorld() != null)
{
moveLeft();
}
}
public void takeDamage()
{
if(canSee(Bullet.class))
{
damage = damage+1;
}
if(damage > 3)
{
getWorld().removeObject(this);
return;
}
}
public void removeAtEdge()
{
if (getX() == 0)
{
getWorld().removeObject(this);
return;
}
}
public void destroyShip()
{
Spaceship spaceship = (Spaceship) getOneIntersectingObject(Spaceship.class);
if (spaceship != null)
{
getWorld().removeObject(spaceship);
Greenfoot.stop();
}
}
public void moveLeft()
{
setLocation(getX()-4,getY());
}
/**
* Return true if we can see an object of class 'clss' right where we are.
* False if there is no such object here.
*/
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
}
import greenfoot.*;
/**
* Write a description of class Bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends Actor
{
private Spaceship spaceship;
/**
* Constructor for a Bullet.
**/
public Bullet(Spaceship spaceship)
{
this.spaceship = spaceship;
}
/**
* Act - do whatever the Bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(5);
vanishOnImpact();
if (getWorld() == null) return;
vanishAtEdge();
}
private void vanishOnImpact()
{
if(canSee(Asteriod.class))
{
getWorld().removeObject(this);
}
}
/**
* If at the edge of the world, then disappear.
**/
private void vanishAtEdge()
{
if (atWorldEdge())
{
getWorld().removeObject(this);
}
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
**/
public boolean atWorldEdge()
{
if(getX() < 10 || getX() > getWorld().getWidth() - 10)
return true;
if(getY() < 10 || getY() > getWorld().getHeight() - 10)
return true;
else
return false;
}
/**
* Return true if we can see an object of class 'clss' right where we are.
* False if there is no such object here.
*/
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
}

