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

2012/10/19

Error when I shoot a man

Anthony_97 Anthony_97

2012/10/19

#
when i try to shoot a man i get this 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. How do I fix this?
erdelf erdelf

2012/10/19

#
maybe you should give us code
davmac davmac

2012/10/19

#
You are calling a method on an actor which relies on the actor's location, but the actor isn't in the world (either it was never added, or it has been removed), so it doesn't have a location. To fix it, you need to avoid calling the method when the actor isn't in the world.
Anthony_97 Anthony_97

2012/10/19

#
so how do I do that. my code for the person i try to shot is: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class NPC_Darkguy here. * * @author (your name) * @version (a version number or a date) */ public class NPC_Darkguy_Horizontaal extends Animal { public int speed = 2; public String direction; public NPC_Darkguy_Horizontaal() { setRotation(0); direction = "left"; } public void act() { setLocation(getX() + speed, getY()); atWall(); if(canSee(Player.class)) { eat(Player.class); } } public void changeDirection() { if (direction.equals("left")) { setRotation(180); direction = "right"; } else { setRotation(0); setLocation(getX()+5, getY()); direction = "left"; } } public void atWall() { Actor wall = getOneIntersectingObject(Wall_block.class); if (wall != null) { speed = -speed; changeDirection(); } } } And my players code is: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Animal { private int snelheid=2; public void act() { if (Greenfoot.isKeyDown("left")) { setLocation(getX() - snelheid, getY()); Actor wallCell = getOneIntersectingObject(Wall_block.class); if (wallCell !=null) { setLocation (getX()+ snelheid, getY()); } Actor treeCell = getOneIntersectingObject(Tree.class); if (treeCell !=null) { setLocation (getX()+ snelheid, getY()); } } if (Greenfoot.isKeyDown("right")) { setLocation(getX() + snelheid, getY()); Actor wallCell = getOneIntersectingObject(Wall_block.class); if (wallCell !=null) { setLocation (getX()- snelheid, getY()); } Actor treeCell = getOneIntersectingObject(Tree.class); if (treeCell !=null) { setLocation (getX()- snelheid, getY()); } } if (Greenfoot.isKeyDown("up")) { setLocation(getX(), getY() - snelheid); Actor wallCell = getOneIntersectingObject(Wall_block.class); if (wallCell !=null) { setLocation (getX(),getY()+snelheid); } Actor treeCell = getOneIntersectingObject(Tree.class); if (treeCell !=null) { setLocation (getX(),getY()+snelheid); } } if (Greenfoot.isKeyDown("down")) { setLocation(getX(), getY() + snelheid); Actor wallCell = getOneIntersectingObject(Wall_block.class); if (wallCell !=null) { setLocation (getX(),getY()-snelheid); } Actor treeCell = getOneIntersectingObject(Tree.class); if (treeCell !=null) { setLocation (getX(),getY()-snelheid); } } // Ik heb de "isKeyDown" code niet gebruikt omdat er dan teveel kogels komen wanneer er geschoten wordt. // Bij deze code komt er maar een kogel wanneer er op "space" gedrukt wordt. if ("space".equals(Greenfoot.getKey())) { fire(); } } private void fire() { Arrow arrow = new Arrow(); getWorld().addObject(arrow, getX(), getY()); } }
danpost danpost

2012/10/19

#
We need to see any (and all) code related to adding and removing the actor to and from the world. That would include your world constructor (or 'prepare' method; or whatever code initially adds the actors to the world) and probably the Arrow class code, as I did not see anything to remove actors in the classes given above, First and foremost, copy/paste the exact error message you were getting (clear the terminal before running the scenario and copy the whole message).
You need to login to post a reply.