使物体在碰撞后消失!JAVA
我正在eclipse中制作一个android游戏,在这个游戏中我有一枚硬币和一个玩家。我希望我的硬币在与我的玩家相撞后消失 我的代码如下所示:使物体在碰撞后消失!JAVA,java,android,eclipse,libgdx,Java,Android,Eclipse,Libgdx,我正在eclipse中制作一个android游戏,在这个游戏中我有一枚硬币和一个玩家。我希望我的硬币在与我的玩家相撞后消失 我的代码如下所示: private void drawCoins(float runtTime){ if (coin1.collides(player)==false && coin2.collides(player)==false && coin3.collides(player)==false){ batc
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());
}
}
在我的硬币课上:
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;
}
最后,在我的游戏世界课程中:
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后立即显示。我强烈建议重新构造代码。虽然一开始可能会有些混乱,但这会使以后的事情变得容易得多
- 如果Coin类持有对其ID的引用,那么列表可能是最简单的选择,但是从ID到Coin的映射也是同样有效的设计选择。(有关集合实现的具体细节,请参见javadocs和。)
- 为了继续讨论,让我们假设您选择将所有硬币对象放在一个列表中
- 如果您以后决定添加另一个硬币,则必须添加更多的条件语句。当您有n枚硬币时,您将有2^n条条件语句用于检查是否要抽取硬币。这会很快变大,并且很容易出错
- 如果您在一个Iterable数据结构中拥有所有硬币(扩展list或Map的所有Java类都是),那么您可以使用for-each循环或
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
}
}
虽然我并不完全熟悉您使用的游戏引擎,但我很有信心实现这两个更改将为您提供所需的“碰撞时消失”功能。我在浏览器中键入了所有这些代码,所以在使用代码之前可能需要进行一些修改
每当你想在当前的舞台/视图/游戏世界中添加新的硬币时,请确保在硬币列表上执行removeAll并手动将其添加回,否则屏幕上的硬币将过多或没有
资料来源:,有很多Java标准库的经验。将资源替换为1px空白、透明的PNG。最简单的方法,还是最简单的方法?两种选择:1。将硬币移到远离屏幕2的地方。当硬币被“重置”时,不要让它自己画出来。我已经解决了一半。每次碰撞为真时,我都会使硬币消失,但现在我在碰撞后立即重新绘制,我将其设置为假
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());
}
}