Java 如何在 Libgdx 中调整精灵的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21382913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize a sprite in Libgdx?
提问by Wazani
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.
我对sprite.setSize(float x, float y)
Libgdx 中的方法有问题。它不会影响精灵的大小或尺寸。无论我传递给 setSize() 方法,它们都保持固定。
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?
你能找出我的错误吗?
采纳答案by Wazani
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.
我不得不使用sprite.draw(batch);
而不是使用,Batch.draw(Sprite sp, float x, float y);
因为该Batch.draw(...)
方法从传递的精灵中获取纹理,并在绘图过程中使用具有固定宽度和固定高度的纹理。
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.
解决这个问题的另一种方法是使用类中的batch.draw(Sprite, float x, float y, float width, float height);
方法SpriteBatch
。