Note: I am using the Greenfoot IDE
So I have been tasked to create a Balloon project where objects of a Balloon class float to the top of the screen. Any other details are up to my decision.
So I made it so that my Balloons pop through a pop method, which is called by two other methods. The two other methods are topPop(), where pop() is called if (getY() <= 0), and hitCopter(), where the pop() is called if the Balloon intersects with my helicopter player object.
The problem here is that in my Act() method, the program crashes after it hits whichever method I called first, hitCopter() or topPop(), and says the other was to blame for the crash, because it attempts to go through both methods but cannot do the other because you can't check for it intersecting or its getY() when it no longer exists in the world.
So I figured that if I could check if that instance of the object was still in the world before performing either topPop() or hitCopter(), it would solve my problem. The issue is that I don't know how to do that and I can't find an answer online about how explicitly to do so.
I attempted to do so through a list, and my code here reflects that, but I don't actually know how to check a list, so my if statements checking that reflect my ignorance.
Here is my code currently:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class Balloon here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Balloon extends Actor
{
//variable declaration
private int yAxis;
private int num;
private int size;
private int health;
private int dmg;
private int rndmSize;
private int setSize;
private Balloon thisBalloon;
public Balloon()
{
rndmSize = Greenfoot.getRandomNumber(6)+1;
rndmSize();
setSize(setSize);
num=0;
yAxis=0;
thisBalloon = this;
}
public void rndmSize()
{
if (rndmSize==1)
{
setSize = 5;
}
else if(rndmSize==2)
{
setSize = 10;
}
else if(rndmSize==3)
{
setSize = 15;
}
else if(rndmSize==4)
{
setSize = 20;
}
else if(rndmSize==5)
{
setSize = 25;
}
else
{
setSize = 30;
}
}
public void act()
{
fly();
if (getWorld().getObjects(Balloon.class) != Balloon.this)
{
topPop();
}
List<Balloon> balloon = this;
for(Actor actor : balloon)
{
if (actor instanceof balloon)
return true;
}
if (getWorld().getObjects(Balloon.class) != Balloon.this)
{
hitCopter();
}
setSize(health);
}
public void fly()
{
num++;
yAxis=getWorld().getHeight()-num;
setLocation(getX(),yAxis);
turn(1);
}
public void setSize(int size2)
{
health = size2;
this.size = size2;
dmg = size2/2;
GreenfootImage image=getImage();
image.scale(size2,size2);
}
public void topPop()
{
if(getY()<=5)
{
pop();
}
}
public void hit(int damage)
{
health-=damage;
if(health <=0)
{
getWorld().removeObject(this);
return;
}
}
public void hitCopter()
{
Helicopter copter = (Helicopter) getOneIntersectingObject(Helicopter.class);
if (copter != null)
{
copter.hit(dmg);
pop();
}
}
public int returnSize()
{
return size;
}
public void pop()
{
//popping animation
//remove the balloon
getWorld().removeObject(this);
}
}