[英]Animate texture drawn in a rectangle in libGDX
在过去的两周里,我一直在学习libGDX,并努力掌握如何使用纹理为运动设置动画。
这是我创建图像的方式:
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();
}
我的纹理是两个并排的图像,每个图像为128 * 256像素,因此纹理大小为256 * 512像素。 我想为矩形设置动画,使其在两个图像之间闪烁。 我已经对此进行了研究,但无法找到任何可行的方法,因为我找不到使用矩形进行此操作的示例。 使用矩形保存动画是否可以做我想做的事情?
我认为您需要将Texture
分成两个可单独渲染的部分。 使用TextureRegion
引用Texture
一部分。 然后传递正确的TextureRegion
进行draw
。 在您的设置中,您需要执行以下操作:
monkeyImage = new Texture(...)
monkeyL = new TextureRegion(monkeyImage, 0, 0, 128, 256);
monkeyR = new TextureRegion(monkeyImage, 128, 0, 128, 256);
在您的render
方法中:
TextureRegion t = someTest ? monkeyL : monkeyR;
batch.draw(t, bucket.x, bucket.y);
如果要为一个角色准备很多图像,则应查看Libgdx Animation
,该Animation
旨在跟踪代表定时动画中使用的图像的多个“关键帧”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.