Load image with libgdx(使用 libgdx 加载图像)
问题描述
我正在尝试在 libgdx 库构建的 java 应用程序上加载两个图像.但是,我加载了背景图像,如果我没有将图像的位置设置为 (0,0),我无法将其他图像加载到屏幕上.例如;我将图像的位置设置为0,0,没有问题.但是,当我将图像的位置设置为 20、0 时,就看不到了.
I am trying to load two images on the java application built by libgdx library. I loaded the background image however, I could not load the other images on to the screen if I am not set the position of the images to (0,0). For example; I set the position of the image to 0,0 and there is no problem. However, when I set the position of the image to 20, 0, it cannot be seen.
batch.draw(Assets.coinRegion, position.x, position.y, 1, 1)
我正在尝试使用上面的代码绘制图像.
I am trying to draw the image with the above code.
谢谢.
obstacle = loadTexture("data/obstacle.png");
obstacleRegion = new TextureRegion(obstacle, 0, 0, 64, 64);
world.obstacle.position.x += 0.001;
batch.draw(Assets.obstacleRegion,
world.obstacle.position.x, world.obstacle.position.y, 1, 1);
推荐答案
TextureRegion 类描述纹理内部的一个矩形,用于仅绘制纹理的一部分.
The TextureRegion class describes a rectangle inside a texture and is useful for drawing only a portion of the texture.
private TextureRegion region;
...
texture = new Texture(Gdx.files.internal("image.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
//if you have 2 images in image.png add new region and specify rectangular:
//region2 = new TextureRegion(texture, 70, 0, 100, 100);
...
batch.begin();
batch.draw(region, 10, 10);
batch.end();
这里的 20,20,50,50 描述了纹理的一部分,然后在 10,10 处绘制.同样可以通过将 Texture 和其他参数传递给 SpriteBatch 来实现,但 TextureRegion 可以方便地使用一个对象来描述两者.
Here the 20, 20, 50, 50 describes the portion of the texture, which is then drawn at 10,10. The same can be achieved by passing the Texture and other parameters to SpriteBatch, but TextureRegion makes it convenient to have a single object that describes both.
SpriteBatch 有许多绘制纹理区域的方法
SpriteBatch has many methods for drawing a texture region
来源:来源
如果您在 1 中包含 2 个图像,则使用多个区域"变量..(region1 = new ... 和 region2 = new...),否则加载 2 个文件并执行文档中所写的相同操作.
if you have 2 images in 1 then use several "region" variables.. (region1 = new ... and region2 = new...), otherwise load 2 files and do the same what written in documentation.
这篇关于使用 libgdx 加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 libgdx 加载图像
基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01