I want to create a RPG Game where an Arrow is shot. If it touches the Enemy it should disappear. This works so far. I want the Enemy to disappear aswell but it does not work.
The Problem appears in the method kill();
This is my code in class Arrow.
Normally nothing happens, but sometimes the treminal says:
ava.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:714)
at greenfoot.Actor.isTouching(Actor.java:979)
at Arrow.töten(Arrow.java:43)
at Arrow.act(Arrow.java:24)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
This message comes after hitting the Enemy. It takes me to the if statement in the kill method
Thank you for help :-)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Arrow here. * * @author (your name) * @version (a version number or a date) */ public class Arrow extends Actor { private int speed; public Arrow() { speed = 8 ; getImage().scale(getImage().getWidth()/ 28 ,getImage().getHeight()/ 8 ); } public void act() { move(speed); kill(); disappear(); } public void disappear() { Actor noob; noob = getOneIntersectingObject(NPC. class ); Actor wall; wall = getOneIntersectingObject(Obstacle. class ); if (isAtEdge() || noob != null || wall!= null ) // Verschwindet am Welt ende un beim auftreffen eines NPC { getWorld().removeObject( this ); } } public void kill() { if ( isTouching(NPC. class )) { Actor mob; mob = new NPC(); getWorld().removeObject(mob); } } } |