当前位置: 代码迷 >> java >> LibGDX:最大屏幕宽度
  详细解决方案

LibGDX:最大屏幕宽度

热度:84   发布时间:2023-07-31 10:59:50.0

我试图限制屏幕的末端,以使到达屏幕末端的子画面(底部)不超过。

@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,使其不超出屏幕末端?

将此插入到您的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;