0
\$\begingroup\$

The problem: When the player hits the left boundary he stops (this is exactly what I want), when he hits the right boundary. He continues until his rectangle's left boundary meets with the right boundary.

Outcome: https://www.youtube.com/watch?v=yuJfIWZ_LL0&feature=youtu.be

My Code

public class Player extends GameObject{

BufferedImageLoader loader;
Texture tex = Game.getInstance();
BufferedImage image;
Animation playerWalkLeft;
private HealthBarManager healthBar;
private String username;
private int width;
private ManaBarManager manaBar;

public Player(float x, float y, ObjectID ID) {
    super(x, y, ID, null);
    loader = new BufferedImageLoader();
    playerWalkLeft = new Animation(5,tex.player[10],tex.player[11],tex.player[12],tex.player[13],tex.player[14],tex.player[15],tex.player[17],tex.player[18]);

}

public void tick(LinkedList<GameObject> object) {

    setX(getX()+velX);
    setY(getY()+velY);
    playerWalkLeft.runAnimation();


}

public void render(Graphics g) {

    g.setColor(Color.BLACK);

    FontMetrics fm = g.getFontMetrics(g.getFont());

    if(username != null) width = fm.stringWidth(username);
    if(username != null){
        g.drawString(username,(int) x-width/2+15,(int) y);
    }


    if(velX != 0){
        playerWalkLeft.drawAnimation(g, (int)x, (int)y);
    }else{
        g.drawImage(tex.player[16], (int)x, (int)y,  null);

    }


    g.setColor(Color.PINK);
    g.drawRect((int)x,(int)y,33,48);
    g.drawRect(0,0,(int)Game.getWalkableBounds().getWidth(), (int)Game.getWalkableBounds().getHeight());


}

@SuppressWarnings("unused")
private Image getCurrentImage() {
    return image;
}

public float getX() {
    return x;
}

public float getY() {
    return y;
}

public void setX(float x) {
    Rectangle gameBoundry = Game.getWalkableBounds();
    if(x >= gameBoundry.getMinX() && x <= gameBoundry.getMaxX()){
        this.x = x;
    }

}

public void setY(float y) { //IGNORE THE SetY please.
            this.y = y;
}

public float getVelX() {
    return velX;
}

public void setHealthBar(HealthBarManager healthBar){
    this.healthBar = healthBar;
}

public HealthBarManager getHealthBar(){
    return healthBar;
}

public float getVelY() {
    return velY;
}

public void setVelX(float velX) {
    this.velX = velX;
}

public void setVelY(float velY) {
    this.velY = velY;
}

public ObjectID getID() {
    return ID;
}

public void setUsername(String playerName) {
    this.username = playerName;

}

public String getUsername(){
    return this.username;

}

public void setManaBar(ManaBarManager manaBar) {
    this.manaBar = manaBar;

}

public ManaBarManager getManaBar(){
    return manaBar;
}

public int getLevel(){
    return 1;
}


public boolean isPlayerInsideBoundry(float x, float y){
    Rectangle boundry = Game.getWalkableBounds();


    if(boundry.contains(x,y)){
        return true;
    }

    return false;
}

}

What I've tried: - Using a method that checks if the game boundary contains player boundary rectangle. This gave me the same result as what the check statement in my setX did.

\$\endgroup\$
0
\$\begingroup\$

either shift the right edge of getWalkableBounds to account for the geometry of the player (requires a new second function for the renderableBounds)

or you shrink the walkable bounds in the setX function

Rectangle gameBoundry = Game.getWalkableBounds();
gameBoundry.width-=PLAYER_WIDTH;
if(x >= gameBoundry.getMinX() && x <= gameBoundry.getMaxX()){
    this.x = x;
}

and do the same in isPlayerInsideBoundry

\$\endgroup\$
  • \$\begingroup\$ I don't seem to fathom your first solution, though I do get the second one, my player width is 33, so I did: gameBoundry.width -= 33; this made an debug output of: prntscr.com/3moto0 here is my full code: pastebin.com/rL5RZXJp Btw, Game.getWalkableBounds() returns the size of the rectangle that the map is embedded in. \$\endgroup\$ – James Century May 26 '14 at 9:11
  • \$\begingroup\$ Oh, now I see why, it was because it wasn't the initial value. Thanks for your answer. I made it so that now it starts of as the initial value, so I minus 33 when I first create the image. Thanks! \$\endgroup\$ – James Century May 26 '14 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.