I have made a 2D Java game where I have a rectangle on a 2D map which has obstacles, I have been setting up collisions with rectangles and they have not been working, after some more experimenting I soon realised that the rectangles I made for my obstacles are moving around with my player, I mean if I set a rectangle on the coordinates 0, 0 it will stay on the top left of my GUI and will move when I move my player, it stays there constantly and not fixed to the map.
I have the following variables set up:
Animation bucky, movingUp, movingDown, movingLeft, movingRight
Image worldMap;
boolean quit = false;
int[] duration = {200, 200};
int buckyPositionX = 0;
int buckyPositionY = 0;
int shiftX = buckyPositionX + 320;
int shiftY = buckyPositionY + 160;
And under my render method I have the following:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
bucky.draw(shiftX, shiftY);//makes him appear at center of map
if(quit==true){
g.drawString("Resume(R)", 250, 100);
g.drawString("Main(M)", 250, 150);
g.drawString("Quit Game(Q)", 250, 200);
if(quit==false){
g.clear();
}
}
}
If I put inside my render method:
g.fillRect(0,0,100,100);
A rectangle will stay in the top left corner of my screen, I am assuming this happens when I set my rectangles up with my variables and try to create collisions and this is why my collisions are not working. The overall question is, how do I edit my code to keep my rectangles stuck to the map and not moving around with my player.