So, I’m attempting to write a Ladder System using Tiled Maps anyway, i’m using Dermetfans collision from his tutorial series, but I’ve edited it a bit.
Here’s the code that matters
private void handleMovement(float delta)
{
// apply gravity
gravityMultiplier = 1f;
if(!((ladderBelow() || ladderAbove())))
{
velocity.y -= gravity * delta;
}
private boolean isLadder(float x, float y) {
Cell cell = interactiveLayer.getCell((int) (x / interactiveLayer.getTileWidth()), (int) (y / interactiveLayer.getTileHeight()));
return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey("ladder");
}
private boolean ladderBelow() {
for(float step = 0; step < playerSprite.getRegionWidth(); step += collisionLayer.getTileWidth() / 2)
if((isLadder(getX() + step, getY())))
return true;
return false;
}
private boolean ladderAbove() {
for(float step = 0; step < playerSprite.getRegionWidth(); step += collisionLayer.getTileWidth() / 2)
if(isLadder(getX() + step, getY() + playerSprite.getRegionHeight()))
return true;
return false;
}
public boolean collidesBottom() {
for(float step = 0; step < playerSprite.getRegionWidth(); step += collisionLayer.getTileWidth() / 2) {
if(isCellBlocked(getX() + step, getY())) {
if(ladderBelow()) {
if(velocity.y < 0 && !Gdx.input.isKeyPressed(Keys.S))
return true;
else
return false;
}
return true;
}
}
return false;
}
@Override
public boolean keyDown(int keycode) {
switch(keycode) {
case Keys.W:
if(canJump) {
velocity.y = playerSpeed / 1.8f;
canJump = false;
}
break;
case Keys.A:
velocity.x = -playerSpeed;
isMoving = true;
break;
case Keys.D:
velocity.x = playerSpeed;
isMoving = true;
break;
case Keys.S:
velocity.y = -playerSpeed;
isMoving = true;
break;
}
return true;
}
Basically, when I get to a ladder, and i tap W or D it flies all the way down it, I guess because it’s checking to see if there’s collision and it says no, so the velocity is never set back to 0, and in turn just multiplies until I hit something with collision? (My guess)
If I tap W, I fly up the ladder, and then “jump” off the top of it, before landing back ontop of the ladder, (But I’m standing ontop of the ladder block, instead of just on the block in general, like I should be. It’s hard to explain, so I have it uploaded for you guys) – Download: https://www.mediafire.com/?h4j943is7uvat5h