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

2013/5/27

Making enemies bounce off walls

Neon147 Neon147

2013/5/27

#
Hey, I am trying to get my enemies bounce of the wall to the opposite direction once it touches the side but i keep getting an error saying "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." My code is this: private int delaytime = 150; public void act() { if(getX() == 1) { setRotation(0); } if(getX() == getWorld().getWidth()-1) { setRotation(180); } if(Greenfoot.getRandomNumber(10)%7==0) { if(delaytime==0) { delaytime=150; getWorld().addObject(new BadLaser(), getX(), getY()); } delaytime--; } }
danpost danpost

2013/5/27

#
Unless you are programmatically calling this 'act' method, the error is not in this part of your code. What is the full error message? The first line that contains a method that you wrote (or that you can edit) is the line in your code the error occurred. The last number in that line is the line number in the class that it occurred.
Neon147 Neon147

2013/5/27

#
Heres the error 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:663) at greenfoot.Actor.getX(Actor.java:157) at Boss1Parts.act(Boss1Parts.java:24) at Boss1Parts.<init>(Boss1Parts.java:20) at doodleLVL3Boss.Boss(doodleLVL3Boss.java:29) at doodleLVL3Boss.<init>(doodleLVL3Boss.java:22) at doodleLVL3Boss.<init>(doodleLVL3Boss.java:13) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at greenfoot.core.Simulation.newInstance(Simulation.java:578) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:408) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:465) at greenfoot.core.Simulation.maybePause(Simulation.java:279) at greenfoot.core.Simulation.runContent(Simulation.java:210) at greenfoot.core.Simulation.run(Simulation.java:203)
danpost danpost

2013/5/27

#
The error is occurring in your 'Boss1Parts' class in the 'act' method while using 'getX' on line 24 of the class. Please copy/paste the first 30 or so lines of that class. The top-most recognizable line for your scenario is: at Boss1Parts.act(Boss1Parts.java:24)
Neon147 Neon147

2013/5/27

#
Sure thing, public class Boss1Parts extends Actor { /** * Act - do whatever the Boss1Parts wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int delaytime = 150; public Boss1Parts() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 1100, image.getHeight()-1100); act(); } public void act() { if(getX() == 1) { setRotation(0); } if(getX() == getWorld().getWidth()-1) { setRotation(180); } if(Greenfoot.getRandomNumber(10)%7==0) { if(delaytime==0) { delaytime=150; getWorld().addObject(new BadLaser(), getX(), getY()); } delaytime--; } } }
MatheMagician MatheMagician

2013/5/27

#
You can't call "act()" in public Boss1Parts(). Delete that part and you will be good.
Neon147 Neon147

2013/5/27

#
Ahhh okay it worked, thanks alot you two!
You need to login to post a reply.