I am currently working on a game and have ran into a problem, I am using the slick built in key listener
and it is (as you have probably already guessed) the X and Y are constantly changing, I am using the Buffered Writer
and when I save the X and Y it saves the initial value of 150 public int X = 150,Y = 150;
when really its set to whatever the coordinates are. I am not sure whats happening, if anyone could help that would be great thanks.
Here's Some Code:
Save Class:
public class saveGame {
BufferedWriter bw;
Player p = new Player();
public void save() {
try {
bw = new BufferedWriter(new FileWriter("saves/saveTest.txt"));
//add save things
bw.write("X: " + p.X);
bw.newLine();
bw.write("Y: " + p.Y);
bw.close();
} catch (IOException e) {e.printStackTrace();}
}
}
Player(p.) Class:
public class Player {
int health;
public int X = 150;
public int Y = 150;
public int saveX;
public int saveY;
}
Play Class:
public class Play extends BasicGameState {
int camX;
int camY;
int WORLD_SIZE_X = 100000;
int WORLD_SIZE_Y = 100000;
int VIEWPORT_SIZE_Y = 768;
int VIEWPORT_SIZE_X = 1024;
int offsetMaxX = WORLD_SIZE_X - VIEWPORT_SIZE_X;
int offsetMaxY = WORLD_SIZE_Y - VIEWPORT_SIZE_Y;
int offsetMinX = 0;
int offsetMinY = 0;
Image im;
//CLASSES//
Player p = new Player();
KeyInput ki = new KeyInput();
saveGame sg = new saveGame();
public Play(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
im = new Image("res/images/play/Char_Up1.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.translate(-camX, -camY);
g.drawImage(im, p.X, p.Y);
g.drawString("X: "+ p.X, 10+camX, 10+camY);
g.drawString("Y: "+ p.Y, 10+camX, 20+camY);
g.drawString("Cam_X: "+ camX, 10+camX, 30+camY);
g.drawString("Cam_Y: "+ camY, 10+camX, 40+camY);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
sg.save();
Input i = gc.getInput();
if(i.isKeyDown(i.KEY_W)) {
p.Y -= 4;
System.out.println("UP!");
}
if(i.isKeyDown(i.KEY_S)) {
p.Y += 4;
System.out.println("DOWN!");
}
if(i.isKeyDown(i.KEY_A)) {
p.X -= 4;
System.out.println("LEFT!");
}
if(i.isKeyDown(i.KEY_D)) {
p.X += 4;
System.out.println("RIGHT!");
}
if(i.isKeyDown(i.KEY_ESCAPE)) {
sg.save();
System.exit(1);
}
camX = p.X - VIEWPORT_SIZE_X / 2;
camY = p.Y - VIEWPORT_SIZE_Y / 2;
if (camX > offsetMaxX){ camX = offsetMaxX; }
if (camX < offsetMinX){ camX = offsetMinX;}
if (camY > offsetMaxY){ camY = offsetMaxY; }
if (camY < offsetMinY){ camY = offsetMinY;}
}
public int getID() {
return 1;
}
}