- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我关注了this tutorial用于在 LibGDX 中构建一个简单的游戏。游戏的基本思想是:有水滴落下,玩家应该通过将水桶拖到水滴落下的地方来放置它们。
在原始版本中,所有水滴都是从一个水滴图像创建的,我尝试做的是制作一系列不同水滴(不同水滴图像)的雨,因此我创建了一个不同Texture
的List
- someDrops
,并在我替换的render()
中:
batch.draw(dropImage, raindrop.x, raindrop.y);
与:
int random = MathUtils.random(0, 3;
batch.draw(someDrops.get(random), raindrop.x, raindrop.y);
我得到的是一场由不同水滴组成的雨,但它们不会分别以不同的图像落下,而是它们落下并同时改变图像。
我如何设置,以便每个水滴都会带有来自 someDrops
的随机图像,因为它以随机的 x,y 位置掉落。我还想为每个掉落添加不同的点,我应该将其保存在哪里以及选择什么类型的集合?
public class Drop implements ApplicationListener {
private Texture dropImage;
private Texture bucketImage;
private Sound dropSound;
private Music rainMusic;
private SpriteBatch batch;
private OrthographicCamera camera;
private Rectangle bucket;
private Array<Rectangle> raindrops;
private long lastDropTime;
private List<Texture> someDrops;
private Texture drop0;
private Texture drop1;
private Texture drop2;
private Texture drop3;
@Override
public void create() {
// load the images for the droplet and the bucket, 64x64 pixels each
dropImage = new Texture(Gdx.files.internal("droplet.png"));
drop0 = new Texture(Gdx.files.internal("droplet0.png"));
drop1 = new Texture(Gdx.files.internal("droplet1.png"));
drop2 = new Texture(Gdx.files.internal("droplet2.png"));
drop3 = new Texture(Gdx.files.internal("droplet3.png"));
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
someDrops = new ArrayList<Texture>();
someDrops.add(new Texture(drop0));
someDrops.add(new Texture(drop1));
someDrops.add(new Texture(drop2));
someDrops.add(new Texture(drop3));
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
// create a Rectangle to logically represent the bucket
bucket = new Rectangle();
bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
bucket.width = 64;
bucket.height = 64;
//raindrops array and spawn the first raindrop
raindrops = new Array<Rectangle>();
spawnRaindrop();
private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = MathUtils.random(0, 800-64);
raindrop.y = 480;
raindrop.width = 64;
raindrop.height = 64;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
// begin a new batch and draw the bucket and
// all drops
batch.begin();
batch.draw(bucketImage, bucket.x, bucket.y);
for(Rectangle raindrop: raindrops) {
//batch.draw(dropImage, raindrop.x, raindrop.y);
int random = MathUtils.random(0, 4);
batch.draw(someDrops.get(random), raindrop.x, raindrop.y);
}
batch.end();
// check if we need to create a new raindrop
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if(raindrop.y + 64 < 0) iter.remove();
if(raindrop.overlaps(bucket)) {
dropSound.play();
iter.remove();
}
}
}
最佳答案
在你的render()
函数中,我相信你每帧都会调用它,你会绘制所有现有的水滴,如果需要的话创建一个新的水滴,并减少水滴的y位置。问题的出现是因为每次你想要绘制现有的水滴时,你都会使用
int random = MathUtils.random(0, 4);
这可能会给你一个与之前绘制的图像不同的图像,即当它处于更高的 y 位置时。因此,当它落下时(y 递减),每次渲染时图像都会不断变化,直到它落入桶中时最终被移除。
要解决此问题,请仅在第一次渲染新水滴时获取随机图像,然后保存该图像并在水滴的整个生命周期中使用相同的图像进行绘制。用于保存相应图像的新数组的大小将与雨滴数组保持相同,除非添加了新的雨滴,在这种情况下您将获得新的随机图像。
我将添加一些代码来说明这一点,但我从未用 Java 编程或使用过 libgdx,因此您必须修复此编译器错误:)
首先创建雨滴纹理
private Array<Texture> raindropTextures; // need an ArrayList here?
在 create() 中添加此 -
raindropTextures = new Array<Texture>();
然后在 render() 中
// if the sizes aren't equal, a new raindrop must have been added!!
if(raindrops.size() != raindropTextures.size()) {
int random = MathUtils.random(0, 4);
raindropTextures.add(someDrops.get(random));
}
for(Rectangle raindrop: raindrops, Texture texture: raindropTextures) {
// make sure the textures and raindrops correspond here; theoritically they should
batch.draw(texture, raindrop.x, raindrop.y);
}
最后 -
Iterator<Rectangle> iter = raindrops.iterator();
Iterator<Texture> iter2 = raindropTextures.iterator();
//later
iter.remove()
iter2.remove()
关于java - 在 render() 中随机更改图像,而不更改所有其他图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870598/
我需要在每个渲染帧完成后拍摄屏幕截图,但我发现某些屏幕截图是重复的,所以我想知道是否可以在渲染完成之前保存屏幕截图。因此... renderer.render() 会阻塞直到完成渲染吗? 如果没有,有
web.py 骨架代码中的“render._keywords['globals']['render'] = render”是什么意思? http://webpy.org/skeleton/0.3 最佳
所以在我的 Nuxt universal-mode 应用程序中,我有时会出现错误: vue.runtime.esm.js:620 [Vue warn]: The client-side rendere
我想创建一个 portal-like effect使用 Bevy . Unity 似乎有一个 render texture实现这一目标。 有没有办法在 Bevy 中做同样的事情?如果没有, futur
我有一个看起来像这样的组件(非常简化的版本): const component = (props: PropTypes) => { const [allResultsVisible, setA
编辑:我调整了代码,但问题仍然存在。见下文 我有这个 p:selectOneRadio : 而这个 p:radioButton : 和 AData包含其
为了渲染部分我可以使用 render 'partial_name' 或 render partial: 'partial_name' 我开始知道 render 是 render partial 的简写
我注意到文章中的一些地方使用了 React.render() 和一些地方 ReactDOM.render()。这两者有什么具体区别吗? 最佳答案 这是 0.14 中引入的最新更改。他们将 React
我的代码是这样的: function render() { renderer.render( scene, camera ); renderer.clear(); } 我想知道为什么它
我目前正在实现 useSWR 以便从我的 express 和 mongo-db 后端获取数据。我能够从数据库中成功获取数据没问题。以下是我用来实现此目的的代码: ```//SWR method for
我只有在按照 React native - "this.setState is not a function" trying to animate background color? 的建议合并了 u
所以我有一个大的纹理,被分成 64x64 block 。 我使用将其加载到 LibGDX texture = new Texture("texturemap.png"); regions = Text
我对放置 @Scripts.Render 和 @Styles.Render 的位置感到很困惑。理想情况下,我会将它们全部放在 head 部分中,但出乎意料的是,例如 @Scripts.Render("
我正在尝试使用 jamon 来收集使用 Tapestry 的网站的统计信息(呈现网页的时间)。 我怎样才能拥有 服务器收到请求时执行的方法,即渲染开始时? 响应全部发送完毕,即渲染结束时执行的方法 ?
在我的 React 应用程序中,我想要渲染一个 prop 值,但直到渲染完成后更新 props 后它才存在。 this.props.users 是一个对象,因此我使用 Object.keys() 转换
我正在使用 React 的钩子(Hook),我希望有一个从数据库中检索到的值作为初始值。但是,我收到以下错误: Invariant Violation: Invariant Violation: Re
我正在尝试按照以下代码将多个场景包含到单个 webgl 渲染器中: renderer.render(scene1, camera); renderer.render(scene2, camera);
我在我的 xhtml 页面中使用此代码,当我运行应用程序时,元描述仍在呈现。我想根据某些条件使用元描述标签。主布局: ..........
我正在使用react-native-render-html来渲染html。renderers方法允许我提供自定义函数来呈现特定标签。不过,我想使用源代码中的原始内部 HTML 将子组件替换为我的自定义
我有一个网格,可以渲染可变高度的卡片。 为了获取卡片的高度,我将卡片渲染在 ReactHeight 中。 ( https://github.com/nkbt/react-height ),这让我可以在
我是一名优秀的程序员,十分优秀!