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

2016/10/31

Error: actor not in world

Mitchell025 Mitchell025

2016/10/31

#
So i recently started programming, so i am kind of a noob. I am making a game for school where i have a cannon, shooting objects at 2 ships which needs to be destroyed when the bullet hits them. This is my code for my bullet: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class kogel here. * * @author (your name) * @version (a version number or a date) */ public class kogel extends Mover { private int life = Greenfoot.getRandomNumber(10) + 20; /** * Act - do whatever the kogel wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10.0); life--; if (life ==0) { getWorld().addObject(new explosie(), getX(), getY()); getWorld().removeObject(this); } Actor schip; schip = getOneObjectAtOffset(0, 0, schip.class); if (schip !=null) { getWorld().removeObject(schip); } } } I get an error "Actor not in world", however the ships disappears as it should so i dont understand why its giving me this error while the game is actually just working fine..
Super_Hippo Super_Hippo

2016/10/31

#
Put the 'if (life==0)' block at the end of the act method. Right now, when life reaches 0, the kogel is removed from the world. After that, you are trying to get an schip object at the position of the kogel, but the kogel is not in the world anymore, so it can't check for something at its coordinates.
You need to login to post a reply.