Hey guys. I'm making a little Megaman vs Bass game for myself where two opponents face off as, you guessed it, Megaman and Bass. I have animated gifs (achieved via the AnimatedActor class) of the two to make the running and jumping seem more realistic. My idea was that if one were to change the direction (aka hit the left arrow key whilst facing right) then I would replace the current Actor with the a new one which would have the same methods and such as the previous, except the animated gif would be facing the opposite way. It compiles perfectly fine, but when I try to do the switch while running the program I get a crazy error, which in turn stops my program. When I hit run again the switch is carried through and I can continue playing, until I try to switch Actors again. I'll attach my Megaman codes that carry out the switch along with the insane error i keep receiving:
(error):
java.lang.IllegalStateException: The actor has not been inserted into a world so it has no location yet. You might want to look at the method addedToWorld on the Actor class.
at greenfoot.Actor.failIfNotInWorld(Actor.java:638)
at greenfoot.Actor.getOneObjectAtOffset(Actor.java:725)
at Bass2.onGround(Bass2.java:84)
at Bass2.checkFall(Bass2.java:74)
at Bass2.act(Bass2.java:23)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:346)
at greenfoot.core.Simulation.run(Simulation.java:178)
(Megaman methods):
private void placeReverse(int x, int y)
{
getWorld().addObject(new MegaMan2(), x, y);
}
private void spawnReverse()
{
locx = getX();
locy = getY();
placeReverse(locx,locy);
getWorld().removeObject(this);
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("a"))
{
spawnReverse();
}
if(Greenfoot.isKeyDown("d"))
{
move(5);
super.act();
}
if(Greenfoot.isKeyDown("w"))
{
jump();
}
}