This site requires JavaScript, please enable it in your browser!
Greenfoot back
KKobra
KKobra wrote ...

2018/4/18

Error with getOneIntersectingObjects

KKobra KKobra

2018/4/18

#
I'm received this error when I "kill" the enemy: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913) at FlyingMod.platformAbove(FlyingMod.java:48) at FlyingMod.act(FlyingMod.java:31) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) this is my code: import greenfoot.*; public class FlyingMod extends Enemies { private int vSpeed = 0; private int acceleration = 1; private boolean jumping; private int jumpStrength = 16; private int speed = 1; private int direction = 1; private int frame = 1; private int animationCounter = 0; private int topX = 300; private int lowerX = 700; private boolean enemyDead = true; /** * Act - do whatever the FlyingMod wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { scrollingMethods(); checkFall(); enemyDie(); platformAbove(); checkRightWalls(); checkLeftWalls(); setLocation(getX() + (direction * speed), getY()); if (getX() < topX && direction == -1) { direction = 1; } if (getX() > lowerX && direction == 1) { direction = -1; } } public boolean platformAbove() { int spriteHeight = getImage().getHeight(); int yDistance = (int)(spriteHeight/-2); Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class); if(ceiling != null) { vSpeed = 1; hitHead(ceiling); return true; } else { return false; } } public boolean checkRightWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/2); Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class); if(rightWall == null) { return false; } else { stopByRightWall(rightWall); return true; } } public void stopByRightWall(Actor rightWall) { int wallWidth = rightWall.getImage().getWidth(); int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2; setLocation(newX - 5, getY()); } public boolean checkLeftWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/-2); Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class); if(leftWall == null) { return false; } else { stopByLeftWall(leftWall); return true; } } public void stopByLeftWall(Actor leftWall) { int wallWidth = leftWall.getImage().getWidth(); int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2; setLocation(newX + 5, getY()); } public void fall() { setLocation(getX(), getY() + vSpeed); if(vSpeed <=9) { vSpeed = vSpeed + acceleration; } jumping = true; } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance = (int)(spriteHeight/2) + 5; Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class); if(ground == null) { jumping = true; return false; } else { moveToGround(ground); return true; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jumping = false; } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public void hitHead(Actor ceiling) { int ceilingHeight = ceiling.getImage().getHeight(); int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2; setLocation(getX(), newY); } public void enemyDie() { Actor player = getOneIntersectingObject (Player.class); if (player!=null) { World myWorld = getWorld(); myWorld.removeObject (this); } } }
danpost danpost

2018/4/18

#
Add:
if (getWorld() == null) return;
after
enemyDie();
in the act method (since it could potentially remove the actor from the world).
KKobra KKobra

2018/4/19

#
The same error
danpost danpost

2018/4/19

#
KKobra wrote...
The same error
Show your revised act method. Also, show the new error ouput.
You need to login to post a reply.