简体   繁体   English

LibGDX:最大屏幕宽度

[英]LibGDX: Max screen width

I'm trying to limit the end of the screen so that the sprite (base) when it reaches the end of the screen does not exceed. 我试图限制屏幕的末端,以使到达屏幕末端的子画面(底部)不超过。

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

How can I limit the posX of the base so it does not exceed the end of the screen? 如何限制基座的posX,使其不超出屏幕末端?

Insert this in your moveBase() method instead of the if(Intersector...) statement: 将此插入到您的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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM