I have a problem with the method sprite.setSize(float x, float y)
in Libgdx. It does not affect the size or the dimensions of the sprite. They remains fixed whatever I pass to the setSize() method.
here is my code:
public class GameScreen implements Screen {
OrthographicCamera camera;
SpriteBatch batch;
Texture carTexture;
Sprite carSprite;
public GameScreen()
{
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
carSprite.setSize(16, 32);
batch.draw(carSprite, 0 , 0);
batch.end();
camera.update();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
camera.viewportWidth=width;
camera.viewportHeight=height;
camera.update();
}
@Override
public void show() {
// TODO Auto-generated method stub
camera = new OrthographicCamera();
batch = new SpriteBatch();
carTexture = new Texture(Gdx.files.internal("NetRace.png"));
carTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
carSprite = new Sprite(carTexture);
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
could you please find my mistake?
A Sprite has a position and a size given as width and height. The position is relative to the origin of the coordinate system specified via Batch. begin() and the respective matrices. A Sprite is always rectangular and its position (x, y) are located in the bottom left corner of that rectangle.
The problem was solved.
I had to use sprite.draw(batch);
instead of using Batch.draw(Sprite sp, float x, float y);
since the Batch.draw(...)
method takes the texture from the passed sprite and uses the texture in the drawing process which has a fixed width and a fixed height.
Another way to solve this problem is to use the batch.draw(Sprite, float x, float y, float width, float height);
method in the SpriteBatch
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With