LibGDX:最大屏幕宽度

时间:2019-02-13 13:27:38

标签: java android libgdx

我正在尝试限制屏幕的末端,以使到达屏幕末端的精灵(底部)不会超过。

@Override
public void create () {
    rectangleBase = new Rectangle();
    endScreen = new Rectangle();
    startScreen = new Rectangle();
    base = new Texture("base1.png");
    deviceHeight = Gdx.graphics.getHeight();
    deviceWidth = Gdx.graphics.getWidth();
    posYBase = (deviceHeight * 20) / 100;
 }

@Override
public void render () {
    moveBase();
    collisionScreenBase();
    spriteBatch.begin();
    drawBase();
    spriteBatch.end();

}
// Here it is used to move the base in pos X with the touch
// I need to make the collision in that method or create another?
void moveBase(){
    posXBase = Gdx.input.getX();
    if(Intersector.overlaps(rectangleBase, endScreen)){
       // Do something
    }
}

void drawBase(){
    spriteBatch.draw(base, posXBase, posYBase);
}


void createCollision(){
    rectangleBase.set(posXBase, posYBase, base.getWidth(),base.getHeight());
    endScreen.set(deviceWidth - 1, 0, 1, deviceHeight);
    startScreen.set(1 + 1, 0, 1, deviceHeight);
}

如何限制基座的posX,使其不超出屏幕末端?

1 个答案:

答案 0 :(得分:0)

在您的moveBase()方法而不是if(Intersector...)语句中插入此内容:

//If the base exceed on the right side
if(Gdx.graphics.getWidth() < posXBase + rectangleBase.getWidth()){
    //set the posXBase as far right as possible without exceed
    posXBase = Gdx.graphics.getWidth() - rectangleBase.getWidth();
//If the base exceed on the left side
else if(posXBase < 0)
    //Set the posXBase to 0
    posXBase = 0;