如何在Libgdx中调整精灵的大小?

2022-09-02 09:10:47

我对Libgdx中的方法有问题。它不会影响子画面的大小或尺寸。无论我传递给 setSize() 方法,它们都是固定的。sprite.setSize(float x, float y)

这是我的代码

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
    }
}

你能找出我的错误吗?


答案 1

问题解决了。

我不得不使用而不是使用,因为该方法从传递的子画面中获取纹理,并在绘制过程中使用具有固定宽度和固定高度的纹理。sprite.draw(batch);Batch.draw(Sprite sp, float x, float y);Batch.draw(...)

解决此问题的另一种方法是在类中使用该方法。batch.draw(Sprite, float x, float y, float width, float height);SpriteBatch


答案 2

推荐