I have created a program that in theory should create a list based on the objects around it, and call a variable from those objects, which would then change the image of those depending on their value. The problem is that I am getting two types of errors that do not make sense to me:
java.lang.IndexOutOfBoundsException: Index: 8, Size: 8 as well as 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.
I cannot for the life of me find out what the first error even means, and what the second error is referring to, as everything that I am doing seems to be in an order that would allow for the calling of the location would work. Here is the code for the Gifts Class:
Any and all help is greatly appreciated as I think I am a bit out of my depth here. Thanks!
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 | import greenfoot.*; import java.util.*; public class Gifts extends Actor { static int amountTouching; //counts how many teachers it is touching int touchingZero; //looks for the adjacent gifts that are not next to a bomb List<Gifts> gifts = new ArrayList<Gifts> (); /** * Act - do whatever the Gifts wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mouseClicked( this )) { amountTouching = getNeighbours( 51 , true , Teacher. class ).size(); checkProximity(); } } public void checkProximity() { if (amountTouching == 0 ) { setImage( "Hill.png" ); checkGifts(); } else { setImage(amountTouching + "adjacent.png" ); } } public void checkGifts() { gifts = getNeighbours( 51 , true , Gifts. class ); int i = 8 ; int checkNumber = 8 ; while (i > 0 ) { if (gifts.get(i).amountTouching == 0 ) { gifts.get(i).setImage( "empty.png" ); } i--; } } } |