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

2012/5/24

Error An attempt was made to use the actors location...

Corfisto Corfisto

2012/5/24

#
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:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Lev1.shot(Lev1.java:139) at Lev1.act(Lev1.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Lev1.shot(Lev1.java:139) at Lev1.act(Lev1.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:656) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:860) at Lev1.shot(Lev1.java:139) at Lev1.act(Lev1.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) Im a bit of a noob at Greenfoot and get this error as soon as the "Boss" shoots me "Lev1" . I checked out a few other posts yet I don't understand them and dont relate to me as much ^_^ This is my code for the "Boss" @Override public void setLocation(int x, int y) {int width = getWorld().getWidth(); int height = getWorld().getHeight(); while (x >= width) { x -= width; } while (x < 0) { x += width; } while (y >= height) { y -= height; } while (y < 0) { y += height; } super.setLocation(x, y); } public void Banana() { List objects = getObjectsInRange(100000, Lev1.class); if (!objects.isEmpty()) { Lev1 lev1 = (Lev1)objects.get(0); int distY = lev1.getY() - getY(); int distX = lev1.getX() - getX(); double angleRadians = Math.atan2(distY, distX); int angleDegrees = (int)Math.toDegrees(angleRadians); setRotation(angleDegrees); move(1); } } public void cat() { sequence ++; switch(sequence) { case 1: bullet bullet = new bullet(); bullet.turn(180); getWorld().addObject(bullet, getX(), getY()); bullet.setRotation(getRotation()); break; case 10: sequence = 0; break; } I hope someone can help me fix this error! Thanks for the help! :D
Corfisto Corfisto

2012/5/25

#
anyone? please? I would appreciate it ^_^
MatheMagician MatheMagician

2012/5/25

#
The error appears to be coming from the Lev1 class, so it would be helpful if you posted the code for that as well.
davmac davmac

2012/5/25

#
See http://www.greenfoot.org/howto/actor-not-in-world.html for an explanation of one of the usual causes of this problem.
Corfisto Corfisto

2012/5/30

#
davmac, i had a look at the code yet it did not seem to help, the problem did not relate to my game sadly. Thanks anyway ^_^. oh this is my "lev1" code public void shot() { Actor Bullet; Bullet = getOneObjectAtOffset (0, 0, bullet.class); if (Bullet != null) { life = life - 1; World world; world = getWorld(); world.removeObject(Bullet); } } public void die() { Actor Bullet; Actor Boss; Bullet = getOneObjectAtOffset (0, 0, bullet.class); Boss = getOneObjectAtOffset (0, 0, boss.class); if (Bullet != null) { if(life < 1) { World world; world = getWorld(); world.removeObject(Boss); world.removeObject(this); } } }
MatheMagician MatheMagician

2012/5/30

#
Your problem is that you are trying to access the boss, yet you do not know where he is! For example you ask
MatheMagician MatheMagician

2012/5/30

#
if(bullet !=null)
yet you do not do the same for the boss. Try
if(boss != null)
         world.removeObject(boss);
davmac davmac

2012/5/30

#
Exception stack trace has: at Lev1.shot(Lev1.java:139) at Lev1.act(Lev1.java:19) So, we really need to see the Lev1 'shot' method (which you have posted) and the 'act' method (which you haven't).
Corfisto Corfisto

2012/5/31

#
ah well this is my "act" method public void act() { move(); spiderEat(); nextLevel(); nextLevel2(); die(); shot(); nextLevel3(); cat(); } I apologise for the stupid "cat" method, it keeps me amused XD Oh mathemagician, i see what you mean yet i have two "if(Bullet !=null)" statements,also, do i replace "if(Bullet !=null) with the "if(boss !=null)" or do i just add it in? thanks guys :D
Corfisto Corfisto

2012/5/31

#
Lev1.shot(Lev1.java:138) at Lev1.act(Lev1.java:21) oh it probably dosent matter but the error has changed to that ^^< probably because i added coding ^_^
MatheMagician MatheMagician

2012/5/31

#
I think you can just put a
if(boss != null)
In front of
world.removeObject(boss);
Corfisto Corfisto

2012/5/31

#
hmm i tried that just then yet now he cant kill me. very unsure :O
davmac davmac

2012/5/31

#
Looks to me that the problem is exactly what I posted about first time. That is, you've got multiple places where the actor is being removed from the world. Once it's removed, you can't access its location or call collision checking methods etc. I refer you to the link I posted earlier. Eg. if calling "die()" ends up removing the actor from the world, you shouldn't call "shot()".
Corfisto Corfisto

2012/6/1

#
right ill give it ago . ^_^
You need to login to post a reply.