0

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.

Rahul Khosla
  • 349
  • 9
  • 21
  • 1
    I think we need some more information here. Is teh world map bigger than the display area of the screen? How are you storing the objects for collision etc. – Vincent Ramdhanie Dec 26 '12 at 22:36
  • I am guessing bucky stays in the middle of the window and the map "moves" beneath it in this case your rectangle will not be moving with bucky more that they are both standing still. instead of (0,0,100,100) you need to use buckyPositionX and buckyPositionY – BevynQ Dec 26 '12 at 22:53
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 27 '12 at 00:06

1 Answers1

0
 if(quit==false){
        g.clear();
 }

section will be never be executed, what is the point of having that if it will be never executed ?

  • I think he thought of setting quit to false when any of the strings is clicked, but he tried to only show necessary code and took this out. – Luatic Feb 04 '17 at 17:42