This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheGrrMan
TheGrrMan wrote ...

2023/9/5

Wall collisions messing up horizontal movement?

TheGrrMan TheGrrMan

2023/9/5

#
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:
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;
  }
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:
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);
  }
Any help would be appreciated; especially if you can explain why it wasn't working before!
danpost danpost

2023/9/7

#
TheGrrMan wrote...
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.
Please clarify: * tile based: how is your world created? please provide the super constructor call line in your world constructor; if the third value is 1, then please explain what you mean by tile based. * moving multiple tiles at a time: does this mean skipping over some tiles (as one move) or making some number of unit moves in succession? It is a bit confusing, you saying it is tile based and the code making it appear not to be.
You need to login to post a reply.