문제

I have been learning libGDX for the past two weeks and struggling to get to grips with how to use a texture to animate movement.

Here is how I create my image:

Rectangle monkey;

public void create() {
    monkeyImage = new Texture(Gdx.files.internal("data/monkey.png"));

    monkey = new Rectangle();
    monkey.x = 800 / 2 - 64 / 2;
    monkey.y = 20;
    monkey.width = 128;
    monkey.height = 256;
}

@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    batch.draw(monkeyImage, bucket.x, bucket.y);
    batch.end();
}

My texture is two images side by side, each image is 128 * 256 pixels, so the texture size is 256 * 512 pixels. I want to animate the rectangle so it flickers between the two images. I have researched this but cannot get anything to work as I cannot find an example that does this using a rectangle. Is it possible to do what I want, using a rectangle to hold an animation?

도움이 되었습니까?

해결책

I think you need to break your Texture in two separately renderable parts. Use a TextureRegion to reference part of a Texture. Then pass the right TextureRegion to draw. In your setup you do something like this:

monkeyImage = new Texture(...)
monkeyL = new TextureRegion(monkeyImage, 0, 0, 128, 256);
monkeyR = new TextureRegion(monkeyImage, 128, 0, 128, 256);

And in your render method:

TextureRegion t = someTest ? monkeyL : monkeyR;
batch.draw(t, bucket.x, bucket.y);

If you are going to have lots of images for one character, you should look at the Libgdx Animation which is designed to keep track of multiple "key frames" that represent the images used in a timed animation.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top