What does the error trace show? (copy/paste)
However, from what I see by the provided code, it looks like you have classing issues (I could be wrong). But, hitAliens does not sound like it should be a class. Also, the act method calls the hitAlien method repeatedly, which has no condition on taking damage. In other words, damage will be taking at all times and zero the health bar in short order (if the error is not arrived at first).
Have the world control game play (adding aliens when needed, etc).
Do not have actors change types in mid-stream. A dead or injured alien is still an alien and one class should control aliens.
Do not use actors that have no useful purpose in the world. If it is not seen and it does not directly interact with any other actors in the world, it should not be in the world and that Actor subclass should not exist.
java.lang.NullPointerException
at Space.hitAlien(Space.java:35)
at Space.act(Space.java:30)
at greenfoot.core.Simulation.actWorld(Simulation.java:600)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:535)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
this is what the error terminal was saying
In my Alien code if i wanted to make it so that if it hits the edge I would lose hp how would I do it
This is my current code
private void atEdge()
{
if (isAtEdge())
{
getWorld().removeObject(this);
}
}
Looks like your healthbar field was not assigned an object. You probably used a local variable inside your world constructor (or prepare method) and not the declared field for the object. So, if you have something like the following in a method:
But i have another question now is there any way i can go into my alien class and make it so that if the aliens are at the edge they will cause me to lose 1 health