Skip to main content
added 1066 characters in body
Source Link

EDIT:

// Set a fix y for you sprite
sprite.setY(fixY);

// Set a max height for your sprite
float maxHeight = Gdx.graphics.getHeight() - sprite.getY(); // Here you say that you want the max height of the sprite to be the difference between screen height and sprite.getY() so it will be in the screen

// Compute your new height
float newHeight = /* Compute new height here */;

// Set your height
sprite.setHeight(MathUtils.clamp(newHeight, 0, maxHeight));

This way :

You fix a Y for your sprite. Then compute the max height of your sprite, depending of his Y position and the screen height. For example :

 sprite.getY() = 80
 Gdx.graphics.getHeight() = 480
 maxHeight = 480 - 80 = 400

Then compute the new height of your sprite. Finally set this with the clamp you compute before.

I already said that before, but you should draw your sprite with sprite.draw(batch) so it really takes it's attributes to be displayed. Here you do some weird things in the batch.draw


Clamp works like this :

You put your value, then min and max, so if your value is superior to max it will return max and if your value is inferior to min it will return min. Here you're setting y by a clamp with min = 0 and max = 700. What you want to do is a clamp between 0 and max = 700 - sprite.getHeight().

In fact, your set Y here will not say anything if you are at Y = 700. But if you're screen height is <= to 700 you won't see it. Because origin is at bot left corner.

If you want your sprite to stay IN screen, you should not clamp on Y, but on setHeight().

What you want is that sprite.getY() + sprite.getHeight() < Gdx.graphics.getHeight()

Clamp works like this :

You put your value, then min and max, so if your value is superior to max it will return max and if your value is inferior to min it will return min. Here you're setting y by a clamp with min = 0 and max = 700. What you want to do is a clamp between 0 and max = 700 - sprite.getHeight().

In fact, your set Y here will not say anything if you are at Y = 700. But if you're screen height is <= to 700 you won't see it. Because origin is at bot left corner.

If you want your sprite to stay IN screen, you should not clamp on Y, but on setHeight().

What you want is that sprite.getY() + sprite.getHeight() < Gdx.graphics.getHeight()

EDIT:

// Set a fix y for you sprite
sprite.setY(fixY);

// Set a max height for your sprite
float maxHeight = Gdx.graphics.getHeight() - sprite.getY(); // Here you say that you want the max height of the sprite to be the difference between screen height and sprite.getY() so it will be in the screen

// Compute your new height
float newHeight = /* Compute new height here */;

// Set your height
sprite.setHeight(MathUtils.clamp(newHeight, 0, maxHeight));

This way :

You fix a Y for your sprite. Then compute the max height of your sprite, depending of his Y position and the screen height. For example :

 sprite.getY() = 80
 Gdx.graphics.getHeight() = 480
 maxHeight = 480 - 80 = 400

Then compute the new height of your sprite. Finally set this with the clamp you compute before.

I already said that before, but you should draw your sprite with sprite.draw(batch) so it really takes it's attributes to be displayed. Here you do some weird things in the batch.draw


Clamp works like this :

You put your value, then min and max, so if your value is superior to max it will return max and if your value is inferior to min it will return min. Here you're setting y by a clamp with min = 0 and max = 700. What you want to do is a clamp between 0 and max = 700 - sprite.getHeight().

In fact, your set Y here will not say anything if you are at Y = 700. But if you're screen height is <= to 700 you won't see it. Because origin is at bot left corner.

If you want your sprite to stay IN screen, you should not clamp on Y, but on setHeight().

What you want is that sprite.getY() + sprite.getHeight() < Gdx.graphics.getHeight()

Source Link

Clamp works like this :

You put your value, then min and max, so if your value is superior to max it will return max and if your value is inferior to min it will return min. Here you're setting y by a clamp with min = 0 and max = 700. What you want to do is a clamp between 0 and max = 700 - sprite.getHeight().

In fact, your set Y here will not say anything if you are at Y = 700. But if you're screen height is <= to 700 you won't see it. Because origin is at bot left corner.

If you want your sprite to stay IN screen, you should not clamp on Y, but on setHeight().

What you want is that sprite.getY() + sprite.getHeight() < Gdx.graphics.getHeight()