This is my code for the bullet and it causes it to disappear but my game suddenly stops. Help?
public void deleteStinger()
{
if(atWorldEdge())
{
getWorld().removeObject(this);
}
}
public void act()
{
setRotation(direction);
move(speed);
move(5);
checkCollision();
deleteStinger();
}
The stinger class extends the animal class in order for atWorldEdge to work. Here is all the code for the stinger and Bee2 (enemy) classes. import greenfoot.*;
import java.util.List;
/**
* Write a description of class Ladybug1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bee2 extends Actor
{
private int beehivesEaten;
private GreenfootImage image3;
private GreenfootImage image4;
/**
* Act - do whatever the Ladybug1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Bee2()
{
beehivesEaten = 0;
image3 = new GreenfootImage("bee.png");
image4 = new GreenfootImage("bee2.png");
setImage(image3);
}
public void act()
{
followBeehive();
lookForBeehive();
switchImage();
}
public void followBeehive()
{
turnTowards(150,500);
Actor beehive = (Actor) getOneIntersectingObject(Beehive.class);
move(3);
}
public void attackBeehive()
{
if(!getObjectsInRange(500, Beehive.class).isEmpty())
{
Actor beehive=(Actor) getObjectsInRange(200, Beehive.class).get(0);
if(beehive !=null)
{
followBeehive();
}
}
}
private void lookForBeehive()
{
if (isTouching(Beehive.class))
{
Greenfoot.playSound("munch.mp3");
removeTouching(Beehive.class);
Greenfoot.setWorld(new GameOverScreen());
Greenfoot.stop();
}
}
public void switchImage()
{
if (getImage() == image3)
{
setImage(image4);
}
else
{
setImage(image3);
}
}
}
// References
//www.youtube.com/watch?v=DoElE2f_QdM
//www.youtube.com/watch?v=aiNg8ChsyUg
//https://www.youtube.com/watch?v=xD98iODedWk
//www.greenfoot.org/topics/60455/0
//https://www.greenfoot.org/topics/60455/0
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 Stinger extends Animal
{
private int direction, speed;
public Stinger(int dir)
{
direction = dir;
speed = 5;
}
public void act()
{
setRotation(direction);
move(speed);
move(5);
checkCollision();
if(atWorldEdge())
{
getWorld().removeObject(this);
}
}
public void checkCollision()
{
Actor bee2 = (Actor) getOneIntersectingObject(Bee2.class);
if(bee2 != null)
{
getWorld().removeObject(bee2);
Fields fields = (Fields)getWorld();
getWorld().removeObject(this);
Greenfoot.playSound("splat.mp3");
Counter counter = fields.obtainCounter();
counter.numberCount(+1); // error I had Counter capitalized giving a nonstatic error
}
}
}
Yes. Lol. I have spent alot of time invested in the game and its finally completed. Its a sigh of relief. However, I still dont understand how myworld can not be equal to nothing.
I still dont understand how myworld can not be equal to nothing.
The method getWorld returns the value of a field that retains the world the actor is currently in. Once you use removeObject(this), that field is set to null. It is not that the world no longer exists; it means that, for all intensive purposes, it does not exist for that actor.