Im using an arraylist in a game for collision detection. I create an object, and put it into the arraylist. When I need to check collisions, or draw the images, I want to be able to call them through the array, not by their original instantiation. The code below is most relevant to the problem.
public class SolidObject { //For counting the array. private int arrayInt; private boolean Solid; private Image Ground1; private boolean Collision; private boolean CollisiontoLeft; private boolean CollisiontoRight; private boolean CollisiontoUp; private boolean CollisiontoDown; private int xPosLeft; private int yPosUp; private int xPosRight; private int yPosDown; private int gamexPos; private Component parent; //Holds all solid objects. static ArrayList <SolidObject> SolidObject = new ArrayList <SolidObject>(); //Instantiates a solid object. (Don't forget to put it in the ArrayList) public SolidObject(int X, int Y, Component parent) { xPosLeft = X; yPosUp = Y; xPosRight = X + Constants.Ground1xSize; yPosDown = Y + Constants.Ground1ySize; gamexPos = X + Constants.Ground1xSize/2; Solid = true; this.parent = parent; } public void setSolidObjectImages(Image Ground1) { this.Ground1 = Ground1; //Needs to be finished. public void paintSolidObject(Graphics gameGraphics) { gameGraphics.drawImage(SolidObject.get(arrayInt).getImage(), SolidObject.get(arrayInt).getxPosLeft(), SolidObject.get(arrayInt).getyPosUp(), parent); } public int getxPosLeft() { return xPosLeft; } public int getyPosUp() { return yPosUp; } public Image getImage() { return Ground1; }
I can't get the image to draw. I'm not very familiar with array lists, so I'm not sure if i may be trying to access values inside the objects wrong or something. I instantiate a new ground object in the main class, and then add it into the arraylist with the add method. Not really sure where to go from here, or why its not working. Eclipse says theres no errors, but somethings not working right lol.