My extended actor
Rock actor
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Base class for rocks
*
*/
public abstract class Rock extends ExtendedActor
{
private final int ROCK_SPEED = 1;
private boolean isRotating, canKill;
private int rotateTarget, immunityTimer;
public Rock()
{
isRotating = false;
canKill = false;
rotateTarget = 0;
immunityTimer = 90;
super.setMoveData( Greenfoot.getRandomNumber(360), ROCK_SPEED );
}
public void act()
{
doRotation();
super.move();
if( checkIntersection() ) {
return;
}
checkBounds();
checkPlayerIntersection(); // bear in mind this can stop execution
if(immunityTimer > 0) {
immunityTimer--;
}
else if ( immunityTimer == 0 ) {
canKill = true;
immunityTimer = -1;
}
}
/**
* Rotate the object randomly
*/
private final void doRotation()
{
if (this.isRotating) {
if ( this.rotateTarget > getRotation() ) {
turn(1);
}
else if ( this.rotateTarget < getRotation() ){
turn(-1);
}
else {
this.isRotating = false;
}
}
else {
this.rotateTarget = Greenfoot.getRandomNumber(360);
this.isRotating = true;
}
}
/**
* Check to see if this intersects with a bullet. Returns true if we do as it means object destruction.
*/
private final boolean checkIntersection()
{
Actor bObj = getOneIntersectingObject(Shot.class);
if(bObj != null) {
// Call this first.. dont want those pesky null pointer exceptions!
onDestroy();
AudioManager.playOnce("laser.wav");
Space world = getSpace();
world.removeObject(bObj);
world.removeObject(this);
return true;
}
return false;
}
/**
* This is called when the rock is destroyed by a bullet
* This is overridden by the bigrock class
*/
public void onDestroy()
{
}
/**
* Check to see if this intersects with a player
*/
public final void checkPlayerIntersection()
{
if( (getOneIntersectingObject(Ship.class) != null) && canKill ) {
Greenfoot.stop();
}
}
/**
* Check to see if this is out of the world
*/
public final void checkBounds()
{
if ( super.isOffWorld() ) {
flipWorldLoc();
}
}
}
big rock code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Big rock class
*
*/
public class BigRock extends Rock
{
public void act()
{
super.act();
}
/**
* Override the default onDestroy method to create rocks
*/
public void onDestroy()
{
getWorld().addObject( new SmallRock(), getX(), getY() );
getWorld().addObject( new SmallRock(), getX() - 14, getY() + 14 );
}
}
//GAME KEEPS CRASHING BECAUSE METEOR IS KILLING ME
small rock code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Small rock class
*
*/
public class SmallRock extends Rock
{
public void act()
{
super.act();
}
}
please fix
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Contains additional methods required by subclasses * */ public abstract class ExtendedActor extends Actor { private double h, v; public ExtendedActor() { h = 0; v = 0; } /** * This turns the actor by a specified ammount * @param ammount The ammount to turn the object by */ public final void turn(int ammount) { setRotation( getRotation() + ammount ); } /** * Flips the actor over so it is on the opposite edge/side of the screen */ public final void flipWorldLoc() { int x = getX(), y = getY(); int newX = getWorld().getWidth() - (x==0 ? 1 : x); int newY = getWorld().getHeight() - (y==0 ? 1 : y); setLocation(newX, newY); } /** * This sets data required to move * @param direction The direction to travel * @param speed The speed to move at */ public final void setMoveData(int direction, int speed) { double angle = Math.toRadians(direction); h = Math.cos(angle) * (double)speed; v = Math.sin(angle) * (double)speed; } /** * This makes us move using the move data */ public final void move() { setLocation( (int)Math.round(getX() + h), (int)Math.round(getY() + v) ); } /** * This method checks the bullets position to see if it is out of the world */ public final boolean isOffWorld() { return ( (getX() <= 1) || (getY() <= 1) || (getX() >= (getWorld().getWidth()-1)) || (getY() >= (getWorld().getHeight()-1)) ); } /** * This returns our world, typecast to space */ public final Space getSpace() { return (Space)getWorld(); } }