I have collision code that works well. You can't move up or down through blocks. There's also code for walls, but whenever I enable them, moving left and right on tiles is messed up. My game is tile-based, and I don't think the code is really meant for moving on multiple tiles at a time. I'll show my code below:
The above checks for up, down, left, and right collisions. I currently have left and right collisions disabled because movement is janky. This is the code that applies the above:
Any help would be appreciated; especially if you can explain why it wasn't working before!
public boolean isOnSolidGround() { //COLLISIONS
boolean isOnGround = false;
if (getY() > getWorld().getHeight() - 50) isOnGround = true;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth / -4, imageHeight / 2, Ground.class) != null ||
getOneObjectAtOffset(imageWidth / 4, imageHeight / 2, Ground.class) != null) {
isOnGround = true;
}
return isOnGround;
}
public boolean didBumpHead() { // COLLISIONS
boolean bumpedHead = false;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth / -4, imageHeight / -2, Ground.class) != null ||
getOneObjectAtOffset(imageWidth / 4, imageHeight / -2, Ground.class) != null) {
bumpedHead = true;
}
return bumpedHead;
}
public boolean canMoveLeft() { //COLLISIONS
boolean canMove = true;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth / -4 - 3, imageHeight / -2, Ground.class) != null ||
getOneObjectAtOffset(imageWidth / -4 - 3, imageHeight / 2 - 1, Ground.class) != null) {
canMove = false;
}
return canMove;
}
public boolean canMoveRight() { //COLLISIONS
boolean canMove = true;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth / 4 + 3, imageHeight / -2, Ground.class) != null ||
getOneObjectAtOffset(imageWidth / 4 + 3, imageHeight / 2 + 1, Ground.class) != null) {
canMove = false;
}
return canMove;
}public void move() { //controls movement
int y = getY();
int x = getX();
if (Greenfoot.isKeyDown("A") && getX() >= 10) {
if (aiming == false) {
started = true;
walkFrames++;
Dir = "Left";
x = x - 3;
}
} else if (Greenfoot.isKeyDown("D") && getX() <= 590) {
if (aiming == false) {
started = true;
x = x + 3;
Dir = "";
walkFrames++;
}
} else {
walkFrames = 0;
}
setLocation(x, y);
}

