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?
Insert this in your moveBase()
method instead of the if(Intersector...)
statement:
//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;