LibGDX: 最大屏幕宽度

LibGDX: Max screen width

我正在尝试限制屏幕的末端,以便精灵(基础)到达屏幕末端时不超过。

@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);
}

如何限制 base 的 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;