gpt4 book ai didi

java - LibGDX 中的碰撞

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:56 25 4
gpt4 key购买 nike

我正在努力让这个游戏正常运行。事实并非如此。

Iterator<Circle> iter = asteroids.iterator();
while (iter.hasNext()) {
Circle asteroid = iter.next();
asteroid.y -= speed * Gdx.graphics.getDeltaTime();
if (asteroid.y + asteroid.radius * 2 < 0)
iter.remove();
if (Intersector.overlapCircles(shipCir, asteroid) && alive) {
iter.remove();
alive = false;

}
}

Iterator<Rectangle> iterB = bullets.iterator();
while (iterB.hasNext()) {
Rectangle bullet = iterB.next();
bullet.y += 300 * Gdx.graphics.getDeltaTime();
if (bullet.y > Gdx.graphics.getHeight())
iterB.remove();

}

/*if (Intersector.overlapCircleRectangle(asteroid, bullet)) {
// REMOVE OBJECT
}
*/

不起作用的部分是上面注释掉的部分。如果你能告诉我如何解决这个问题,那就太棒了。它不会让我访问小行星或子弹,除非我在 while 循环内并且我不知道为什么。

更新:我认为这是你告诉我要尝试的。它仍然在做同样的事情。

Iterator<Circle> iter = asteroids.iterator();
Iterator<Rectangle> iterB = bullets.iterator();

while (iter.hasNext()) {
Circle asteroid = iter.next();
asteroid.y -= speed * Gdx.graphics.getDeltaTime();
if (asteroid.y + asteroid.radius * 2 < 0)
iter.remove();
if (Intersector.overlapCircles(shipCir, asteroid) && alive) {
iter.remove();
alive = false;
if (exploding) {
exploding = false;
explode.start();
}
}
while (iterB.hasNext()) {
Rectangle bullet = iterB.next();
bullet.y += 300 * Gdx.graphics.getDeltaTime();
if (bullet.y > Gdx.graphics.getHeight())
iterB.remove();
}
}

更新:我偶尔会在“iterB.remove();”上遇到错误我不知道为什么。

代码:

Iterator<Circle> iter = asteroids.iterator();
while (iter.hasNext()) {
Circle asteroid = iter.next();
asteroid.y -= speed * Gdx.graphics.getDeltaTime();
if (asteroid.y + asteroid.radius * 2 < 0)
iter.remove();
if (Intersector.overlapCircles(shipCir, asteroid) && alive) {
iter.remove();
alive = false;
if (!exploding) {
exploding = true;
explode.start();
}
}
Iterator<Rectangle> iterB = bullets.iterator();
while (iterB.hasNext()) {
Rectangle bullet = iterB.next();
bullet.y += 150 * Gdx.graphics.getDeltaTime();

if (Intersector.overlapCircleRectangle(asteroid, bullet)) {
iterB.remove();
iter.remove();

}
if (bullet.y > Gdx.graphics.getHeight())
iterB.remove(); // ERROR HERE
}
}

错误如下:

 Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ArrayIndexOutOfBoundsException: -1
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111)
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at com.badlogic.gdx.utils.Array.removeIndex(Array.java:216)
at com.badlogic.gdx.utils.Array$ArrayIterator.remove(Array.java:433)
at com.chanceleachman.space.Screens.GameScreen.render(GameScreen.java:140)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.chanceleachman.space.Space.render(Space.java:37)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:190)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:108)

最佳答案

asteroidbullet 变量的作用域为它们各自定义的 while 循环,因此不能在这些作用域之外使用。您应该回顾一下 Java variable scoping .

您可以通过将 bulletasteroid 的声明提升到 while 循环之外(即 Circle asteroid; Rectanglebullet;)来修复编译器错误,但我认为生成的代码不会执行您想要的操作(并且在没有子弹或没有小行星的情况下会崩溃)。

需要考虑没有子弹(或没有小行星)的情况,需要考虑有多个子弹和多个小行星的情况。您需要检查每颗子弹与每颗小行星的关系,看看它们是否相交。

关于java - LibGDX 中的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com