碰撞后使物体消失!Java语言



我正在eclipse中制作一个android游戏,在这个游戏中我有一个硬币和一个玩家。我希望我的硬币在与我的玩家碰撞后消失。

我的代码如下:

在我的硬币类:

public boolean collides(Player player){
          if (position.x < player.getX() + player.getWidth()){
              return (Intersector.overlaps(player.getBoundingCircle(),coinCircle)||
                      Intersector.overlaps(coinCircle,player.getRect()));
          }
              return false;
      }

在另一个称为滚动器的类中:

public boolean collidesCoin(Player player){
          return (coin1.collides(player)||coin2.collides(player)||coin3.collides(player));
      }

最后是我的游戏世界课程:

  if (scroller.collidesCoin(player)){
                addScore(1);
                 coin.reset(0);
              countcoin=1;

              }

正是在这最后一段代码中,我希望硬币消失。

有什么建议吗?

编辑:我更改了硬币的绘图代码,使其看起来像这样:

private void drawCoins(float runtTime){
        if (coin1.collides(player)==false && coin2.collides(player)==false && coin3.collides(player)==false){
        batcher.draw(coinAnimation.getKeyFrame(runtTime), coin1.getX(), coin1.getY(), coin1.getWidth(), coin1.getWidth());
        batcher.draw(coinAnimation.getKeyFrame(runtTime), coin2.getX(), coin2.getY(), coin2.getWidth(), coin2.getWidth());
        batcher.draw(coinAnimation.getKeyFrame(runtTime), coin3.getX(), coin3.getY(), coin3.getWidth(), coin3.getWidth());

        }else if (coin1.collides(player)==true && coin2.collides(player)==false && coin3.collides(player)==false){
            batcher.draw(coinAnimation.getKeyFrame(runtTime), coin2.getX(), coin2.getY(), coin2.getWidth(), coin2.getWidth());
            batcher.draw(coinAnimation.getKeyFrame(runtTime), coin3.getX(), coin3.getY(), coin3.getWidth(), coin3.getWidth());

        }else if (coin1.collides(player)==false && coin2.collides(player)==true && coin3.collides(player)==false){
            batcher.draw(coinAnimation.getKeyFrame(runtTime), coin1.getX(), coin1.getY(), coin1.getWidth(), coin1.getWidth());
            batcher.draw(coinAnimation.getKeyFrame(runtTime), coin3.getX(), coin3.getY(), coin3.getWidth(), coin3.getWidth());

        }else if (coin1.collides(player)==false && coin2.collides(player)==false && coin3.collides(player)==true){
            batcher.draw(coinAnimation.getKeyFrame(runtTime), coin2.getX(), coin2.getY(), coin2.getWidth(), coin2.getWidth());
            batcher.draw(coinAnimation.getKeyFrame(runtTime), coin3.getX(), coin3.getY(), coin3.getWidth(), coin3.getWidth());
        }
    }

现在,当我的一个硬币的碰撞设置为true时,硬币会消失,但在碰撞设置为false 后立即显示

我强烈建议重组您的代码。虽然一开始可能会有一些混乱,但以后会让事情变得更容易

  1. 我的第一个建议是将硬币保存在动态大小的收藏中,如列表或地图。
    • 如果Coin类包含对其ID的引用,那么List可能是最简单的选择,但从ID到Coin的Map也是同样有效的设计选择。(请参阅ArrayList的javadocs和Collection实现特定的HashMap。)
    • 为了继续讨论,让我们假设您选择将所有硬币对象放在列表中
  2. 不要为每个硬币手动编写代码。
    • 如果你以后决定再添加一枚硬币,你必须添加更多的条件语句。当你有n个硬币时,你将有2^n个条件语句来检查是否要提取硬币。它变得非常大,非常快,而且很容易出错
    • 如果您在Iterable数据结构中有所有的硬币,所有扩展Collection list list或Map的Java类都是Iterable,那么您可以使用for each循环或迭代器

假设你有一个名为coinList:的ArrayList

Iterator<Coin> iterator = coinList.iterator(); // Allows us to traverse list
while (iterator.hasNext()) {        // If we use a for-each here, we can't remove coins.
    Coin coin = iterator.next();    // In the first iteration this will be the 1st element, then second, etc.
    if (coin.collides(player)) {    // Your collision logic can go here
        iterator.remove();          // Removes coin from list when collided with player (we don't want it anymore).
        addScore(1);
        // Do other stuff here when coin collides with player
    }
}

因为你有一个可以迭代的硬币集合,所以你不必手动为每个硬币写代码。

private void drawCoins(float runtTime){
    for (Coin coin : coinList) { // For every coin in the coin list do the following.
        // Because we removed coins when they collided, the only things in the list will be the ones that haven't been collided with yet.
        batcher.draw(coinAnimation.getKeyFrame(runtTime), coin.getX(), coin.getY(), coin.getWidth(), coin.getWidth());
    }
}

虽然我对你使用的游戏引擎并不完全熟悉,但我很有信心实现这两项更改将为你带来你想要的碰撞消失功能。我在浏览器中键入了所有这些代码,所以在使用您的代码之前可能需要进行一些修改。

每当你想向当前舞台/view/gameworld添加新硬币时,请确保在硬币列表中删除所有硬币,然后手动将其添加回,否则你会在屏幕上看到太多或没有硬币。

来源:Oracle Java 8 Javadoc参考资料,丰富的Java标准库经验。

最新更新