Still waiting on the entire bullet class code.
Actor x_wing = getOneIntersectingObject(X_wing.class);
if(x_wing != null) {
World myWorld = getWorld();
myWorld.removeObject(x_wing);
Space space = (Space)myWorld;
Counter counter = space.getCounter();
counter.addScore();
myWorld.removeObject(this);
}
else if(hitTheEdge() !=null) {
World myWorld = getWorld();
myWorld.removeObject(this);
} /*
* A method that tells if I hit the edge and what edge i hit
*/
public String hitTheEdge() {
int x = getX();
int y = getY();
World myWorld = getWorld();
int rightSide = myWorld.getWidth() - 1;
int bottomSide = myWorld.getHeight() -1;
if(y == 0) {
return "top";
}
else if (x == 0) {
return "left";
}
else if(x == rightSide) {
return "right";
}
else if(y == bottomSide) {
return "bottom";
}
else {
return null;
}
}My whole bullet class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bullet extends Actor
{
int time=10;
public boolean atWorldEdge()
{
if(getX() < 10 || getX() > getWorld().getWidth() - 10)
return true;
if(getY() < 10 || getY() > getWorld().getHeight() - 10)
return true;
else
return false;
}
/**
* 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()
{
destroyEnemies ();
move(5);
kill();
if (this.atWorldEdge()) {
getWorld().removeObject(this);
}
}
public bullet()
{
GreenfootImage image=getImage();
image.scale(40,6);
setImage(image);
}
public void destroyEnemies()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor enemy = getOneIntersectingObject(enemy.class);
if(enemy != null)
{
World myWorld = getWorld();
getWorld().removeObject(enemy);
Level1 level1 = (Level1)myWorld;
Counter counter = level1.getCounter();
counter.addScore();
}
}
public void kill()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor blueenemy = getOneIntersectingObject(blueenemy.class);
if(blueenemy != null)
{
getWorld().removeObject(blueenemy);
}
}
}
java.lang.ClassCastException: Level2 cannot be cast to Level1 at bullet.destroyEnemies(bullet.java:52) at bullet.act(bullet.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Level2 cannot be cast to Level1 at rocket.hitenemy(rocket.java:75) at rocket.act(rocket.java:45) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bullet extends Actor
{
int time=10;
public boolean atWorldEdge()
{
if(getX() < 10 || getX() > getWorld().getWidth() - 10)
return true;
if(getY() < 10 || getY() > getWorld().getHeight() - 10)
return true;
else
return false;
}
/**
* 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()
{
destroyEnemies ();
move(5);
kill();
if (this.atWorldEdge()) {
getWorld().removeObject(this);
}
}
public bullet()
{
GreenfootImage image=getImage();
image.scale(40,6);
setImage(image);
}
public void destroyEnemies()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor enemy = getOneIntersectingObject(enemy.class);
if(enemy != null)
{
World myWorld = getWorld();
getWorld().removeObject(enemy);
Level1 level1 = (Level1)myWorld;
Counter counter = level1.getCounter();
counter.addScore();
if (this.atWorldEdge()) {
getWorld().removeObject(this);
}
}
}
public void kill()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor blueenemy = getOneIntersectingObject(blueenemy.class);
if(blueenemy != null)
{
getWorld().removeObject(blueenemy);
}
}
}