Sorry guys, I don't know if the title is the best way to explain it. I'm making a pacman clone in java with libgdx. I've got the map rendered and collision detection on the tiles working. The only problem, the pacman is suppose to keep moving no matter what. When he is going right, and you press down or up which is blocked, he stops. I've tried a bunch of different things and can't seem to get it to work right.
This is my pacman player class
package com.psillidev.pacman1;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
public class player {
private static int height = 16;
private static int width = 16;
private int playerX , playerY;
private int direction;
private Texture playerTexture;
private boolean isAlive;
public player(int startX,int startY) {
playerTexture = new Texture(Gdx.files.internal("data/pacman.png"));
playerX = startX;
playerY = startY;
}
public void move(Map map) {
if (direction == 0) {
if (isNoTile(map,direction,getX(),getY())) {
setY(getY() + (int)(Gdx.graphics.getDeltaTime() + 2));
}
}
if (direction ==1) {
if (isNoTile(map,direction,getX(),getY())) {
setX(getX() + (int)(Gdx.graphics.getDeltaTime() + 2));
}
}
if (direction ==2) {
if (isNoTile(map,direction,getX(),getY())) {
setY(getY() - (int)(Gdx.graphics.getDeltaTime() + 2));
}
}
if (direction ==3) {
if (isNoTile(map,direction,getX(),getY())) {
setX(getX() - (int)(Gdx.graphics.getDeltaTime() + 2));
}
}
}
private boolean isNoTile(Map map, int dir, int translated_x, int translated_y) {
System.out.println("X: " + translated_x + " Y: " + translated_y);
for (Rectangle r : map.getBlockedTiles()) {
switch (dir) {
case 0:
Rectangle up = new Rectangle(translated_x,translated_y + 2 , 16, 16);
if (up.overlaps(r)) {
return false;
}
break;
case 1:
Rectangle right = new Rectangle(translated_x+2,translated_y, 16, 16);
if (right.overlaps(r)) {
return false;
}
break;
case 2:
Rectangle down = new Rectangle(translated_x,translated_y - 2, 16, 16);
if (down.overlaps(r)) {
return false;
}
break;
case 3:
Rectangle left = new Rectangle(translated_x-2,translated_y, 16, 16);
if (left.overlaps(r)) {
return false;
}
break;
default:
break;
}
}
return true;
}
public void render(SpriteBatch batch) {
batch.draw(playerTexture, playerX, playerY);
}
public int getX() {
return playerX;
}
public void setX(int x) {
playerX = x;
}
public int getY() {
return playerY;
}
public void setY(int y) {
playerY = y;
}
public int getDirection() {
return direction;
}
public void setDirection(int dir) {
direction = dir;
}
public Rectangle getRect() {
return new Rectangle(playerX,playerY,width,height);
}
}