I have a problem in my game. My player cannot move after I put collision detection from this video: https://www.youtube.com/watch?v=DadImcUt9Nk&index=22&list=PLah6faXAgguMnTBs3JnEJY0shAc18XYQZ
And I made my Mario like game using these videos. I followed all the things said in the video and the whole series from episode 1 to 22. before implementing the code from episode 22, my game worked fine. But after implementing the code for collision detection, my player stopped moving. And I checked through the code and I didn't find any error. I hope you guys can help me on this as I'm new to coding.
Thanks :D
My code
(EntityClass)
protected Handler handler;
protected float x, y;
protected int width, height;
protected Rectangle bounds;
public Entity(Handler handler, float x, float y, int width, int height){
this.handler = handler;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
bounds = new Rectangle(0, 0, width, height);
}
public abstract void tick();
public abstract void render(Graphics g);
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
(WorldClass)
private Handler handler;
private int width, height;
private int spawnX, spawnY;
private int[][] tiles;
public World(Handler handler, String path){
this.handler = handler;
loadWorld(path);
}
public void tick(){
}
public void render(Graphics g){
int xStart = (int) Math.max(0, handler.getGameCamer().getxOffset() / Tile.TILEWIDTH);
int xEnd = (int) Math.min(width, (handler.getGameCamer().getxOffset() + handler.getWidth()) / Tile.TILEWIDTH + 1);
int yStart = (int) Math.max(0, handler.getGameCamer().getyOffset() / Tile.TILEHEIGHT);
int yEnd = (int) Math.min(height, (handler.getGameCamer().getyOffset() + handler.getHeight()) / Tile.TILEHEIGHT +1);
for(int y = yStart;y < yEnd;y++){
for(int x = xStart;x < xEnd;x++){
getTile(x, y).render(g, (int) (x * Tile.TILEWIDTH - handler.getGameCamer().getxOffset()),
(int) (y * Tile.TILEHEIGHT - handler.getGameCamer().getyOffset()));
}
}
}
public Tile getTile(int x, int y){
if(x < 0 || y < 0 || x >= width || y >= height)
return Tile.grassTile;
Tile t = Tile.tiles[tiles[x][y]];
if(t == null)
return Tile.grassTile;
return t;
}
private void loadWorld(String path){
String file = Utils.loadFileAsString(path);
String[] tokens = file.split("\\s+");
width = Utils.parseInt(tokens[0]);
height = Utils.parseInt(tokens[1]);
spawnX = Utils.parseInt(tokens[2]);
spawnY = Utils.parseInt(tokens[3]);
tiles = new int[width][height];
for(int y = - 0;y < height;y++){
for(int x = 0;x < width;x++){
tiles[x][y] = Utils.parseInt(tokens[(x + y * width) + 4]);
}
}
}
}
(CreaturesClass)
public static final int DEFAULT_HEALTH = 1;
public static final float DEFAULT_SPEED = 3.0f;
public static final int DEFAULT_CREATURE_WIDTH = 128,
DEFAULT_CREATURE_HEIGHT = 128;
protected int health;
protected float speed;
protected float xMove, yMove;
public Creatures(Handler handler, float x, float y, int width, int height) {
super(handler, x, y, width, height);
health = DEFAULT_HEALTH;
speed = DEFAULT_SPEED;
xMove = 0;
yMove = 0;
}
public void move(){
moveX();
moveY();
}
public void moveX(){
if(xMove > 0){//Moving right
int tx = (int) (x + xMove + bounds.x + bounds.width) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) &&
!collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)){
x += xMove;
}
}else if(xMove < 0){//Moving left
int tx = (int) (x + xMove + bounds.x) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) &&
!collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)){
x +=xMove;
}
}
}
public void moveY(){
if(yMove < 0){//Up
int ty = (int) (y + yMove + bounds.y) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEWIDTH, ty) &&
!collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)){
y += yMove;
}
}else if(yMove > 0){//Down
int ty = (int) (y + yMove + bounds.y + bounds.height) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEWIDTH, ty) &&
!collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)){
y += yMove;
}
}
}
protected boolean collisionWithTile(int x, int y){
return handler.getWorld().getTile(x, y).isSolid();
}
//GETTERS AND SETTERS
public float getxMove() {
return xMove;
}
public void setxMove(float xMove) {
this.xMove = xMove;
}
public float getyMove() {
return yMove;
}
public void setyMove(float yMove) {
this.yMove = yMove;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
}
(PlayerClass)
public Player(Handler handler, float x, float y) {
super(handler, x, y, Creatures.DEFAULT_CREATURE_WIDTH, Creatures.DEFAULT_CREATURE_HEIGHT);
bounds.x = 32;
bounds.y = 32;
bounds.width = 92;
bounds.height = 96;
}
@Override
public void tick() {
getInput();
move();
handler.getGameCamer().centerOnEntity(this);
}
private void getInput(){
xMove = 0;
yMove = 0;
if(handler.getKeyManager().up)
yMove = -speed;
if(handler.getKeyManager().down)
yMove = speed;
if(handler.getKeyManager().left)
xMove = -speed;
if(handler.getKeyManager().right)
xMove = speed;
}
@Override
public void render(Graphics g) {
g.drawImage(Assets.DerpDino, (int) (x - handler.getGameCamer().getxOffset()), (int) (y - handler.getGameCamer().getyOffset()), width, height, null);
g.setColor(Color.red);
g.fillRect((int) (x + bounds.x - handler.getGameCamer().getxOffset()),
(int) (y + bounds.y - handler.getGameCamer().getyOffset()),
bounds.width, bounds.height);
}
}