The problem is about creating objects on the map. Everything would be fine, but suddenly I can only create the objects (which are images), on the screen. Whenever I give some cordination to an object it takes the (0,0) point of the screen, not the map. So I can move my guy around the map (camera works correctly), but for example spider stays on screen, instead on the map. Here intialization of objects:
static float PlayerPositionX = 0;
static float PlayerPositionY = 0;
static GameContainer gc;
static StateBasedGame sbg;
//Spider
Monster monster = new Monster();
float shiftXSpider;
float shiftYSpider;
//Player position at the middle of the screen *half of the screen
static float shiftX = PlayerPositionY + (Game.screenWidth/2);
static float shiftY = PlayerPositionX + (Game.screenHeight/2);
static float shiftXMap = PlayerPositionX;
static float shiftYMap = PlayerPositionY;
This is my render method for map, spider and player. (Just a part, of course)
worldMap.draw(PlayerPositionX,PlayerPositionY);
player.draw(shiftX,shiftY);
spider.draw(spiderPoint.x, spiderPoint.y);
Update method:
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
System.out.println(playerPoint.getX() + " " + playerPoint.getY());
playerPoint.x=PlayerPositionX;
playerPoint.y=PlayerPositionY;
Input input = gc.getInput();
monster.Chase(playerPoint.x, playerPoint.y,delta);
// monster.Chase(PlayerPositionX, PlayerPositionY,delta);
// shiftXSpider=monster.getMonsterPositionX();
// shiftYSpider=monster.getMonsterPositionY();
spiderPoint.x=monster.getMonsterPositionX();
spiderPoint.y=monster.getMonsterPositionY();
if(input.isKeyDown(input.KEY_DOWN)) {
player=movingDown;
PlayerPositionY-=delta * .1f;
if(PlayerPositionY<-600) {
PlayerPositionY+=delta * .1f;
}
}
//move up
if(input.isKeyDown(input.KEY_UP)) {
player=movingUp;
PlayerPositionY+=delta * .1f;
if(PlayerPositionY>162) {
PlayerPositionY-=delta * .1f;
}
}
//move right
if(input.isKeyDown(input.KEY_RIGHT)) {
player=movingRight;
PlayerPositionX-=delta * .1f;
if(PlayerPositionX<-840) {
PlayerPositionX+=delta * .1f;
}
}
//move left
if(input.isKeyDown(input.KEY_LEFT)) {
player=movingLeft;
PlayerPositionX+=delta * .1f;
if(PlayerPositionX>324) {
PlayerPositionX-=delta * .1f;
}
}
}
I did this with a camera class.
Here you can find all my classes: http://pastebin.com/u/huneater
My little game (unfinished): https://www.dropbox.com/s/dhvf5vbqj25sgyw/game.jar
My rendering for an example rectangle:
@Override
public void render(GameContainer gc, Graphics g, Camera camera) {
g.setColor(color);
g.fillRect(x - camera.getX(), y - camera.getY(), width, height);
}
For this camera class you should set the targetX & targetY positions in your players update method:
float targetX = x - (gc.getWidth() / 2);
camera.setTargetX(targetX);
float targetY = y - (gc.getHeight() / 2);
camera.setTargetY(targetY);
And then in your world's update loop you have to call the camera's update loop. So with this, ive got a player in the midlle of my screen, with some blocks in map. And they are perfectly positioned.