Hi everyone, I'm new to Java and a relative newbie when it comes to programming in general. I've been learning a bit from some Youtube tutorials dealing with Java basics. They've been very helpful and I've been able to create a basic game setup with my new knowledge, with a randomly generated map of tiles, a "camera" that allows the user to look around the map, and a character that the use can move using the arrow keys. I have a few problems/questions though.
1) Regarding classes/superclasses. I have an abstract class, "Tile", which contains methods such as getImage and getPassable, with subclasses "Grass" and "Rock". However, I seem to have to write the methods from "Tile" in the subclasses as well to get them to work, which was not how I thought it worked. Am I doing something wrong? Tile class below:
package main;
import java.awt.Image;
public abstract class Tile {
private Image tileImage;
private boolean tilePassable;
private int x,y;
public Image getImage(){
return tileImage;
}
public boolean getPassable(){
return tilePassable;
}
public int getHeight(){
return tileImage.getHeight(null);
}
public int getWidth(){
return tileImage.getHeight(null);
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
2) Character movement. I want my character to move along the tiles, so it should not be able to stop when it's half way between two tiles. I use the following methods:
//sets player movement, is called when users presses an arrow key
public void playerMove(int d, int m, float vx, float vy){
if(player.getMoveDistance() == 0){
player.setDirection(d);
player.setMoveDistance(m);
player.setVelocityX(vx);
player.setVelocityY(vy);
}
}
//update method in the sprite, is called in the main game loop
public void update(long timePassed) {
if(moveDistance > 0){
x += vx * 32;
y += vy * 32;
if(Math.round(x) % 32 == 0 && Math.round(y) % 32 == 0){
moveDistance--;
}
}
}
Basically, moveDistance specifies the number of tiles left to move, and the velocity cannot be changed if this is not 0. However, this seems to generate quite a large wait if the player wishes to change direction - the sprite reaches the end of the tile, then does nothing for around half a second before taking the new direction. Is there a better method to achieve the movement I want or am I stuck with this delay?
Thanks in advanced for any help.