How to resize a sprite in Libgdx?(如何在 Libgdx 中调整精灵的大小?)
问题描述
我对 Libgdx 中的方法 sprite.setSize(float x, float y)
有问题.它不会影响精灵的大小或维度.无论我传递给 setSize() 方法,它们都保持不变.
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.
这是我的代码:
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
}
}
你能找出我的错误吗?
推荐答案
问题解决了.
我不得不使用 sprite.draw(batch);
而不是使用 Batch.draw(Sprite sp, float x, float y);
因为 Batch.draw(...)
方法从传入的精灵中获取纹理,并在绘制过程中使用具有固定宽度和固定高度的纹理.
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.
解决这个问题的另一种方法是使用SpriteBatch
中的batch.draw(Sprite, float x, float y, float width, float 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.
这篇关于如何在 Libgdx 中调整精灵的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Libgdx 中调整精灵的大小?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01