Hello again greenfoot community,
if anyone remembers I asked for help yesterday on this game but I have come across another issue. I now have arrows in my game that I want to destroy viruses on the screen. I want the arrow to disappear on contact with the virus and the virus to disappear after it is hit with 2 arrows. I used the canSee method for both and for some reason it works for the arrow but not for the virus.
Here is the code for both the arrow and virus
And here are the issues I am getting
Here the arrow is touching the virus
for the arrow, the method returns true
however for the virus, it returns false
public class Arrow extends Actor
{
private int speed;
private int arrowCounter;
public Arrow(int x, int y, int s, int d)
{
setLocation(x,y);
speed = s;
arrowCounter = 10;
if(d == 4)
{
setRotation(0);
}
else if(d==1)
{
setRotation(90);
}
else if(d==2)
{
setRotation(180);
}
else if(d==3)
{
setRotation(270);
}
}
public void act()
{
move(speed);
if(atWorldEdge()) //|| isOn(Virus.class))
{
getWorld().removeObject(this);
}
if(isOn(Virus.class))
{
speed = 0;
arrowCounter --;
if(arrowCounter == 0)
{
getWorld().removeObject(this);
}
}
}
public boolean isOn(Class clss)//this is the same as the canSee method
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
}public class Virus extends Animal
{
private int damageCount;
public Virus()
{
super();
damageCount = 0;
}
public void act()
{
checkForArrows();
checkDeath();
}
public void checkForArrows()
{
if(canSee(Arrow.class))
{
damageCount++;
}
}
public void checkDeath()
{
if(damageCount == 2)
{
getWorld().removeObject(this);
}
}
}
Here the arrow is touching the virus
for the arrow, the method returns true
however for the virus, it returns false
