我在日食中制作一款安卓游戏,在那款游戏中我有一枚硬币和一名玩家。我希望我的硬币在与我的播放器发生碰撞后消失。
我的代码如下:
在我的硬币课上:
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;
}
在另一个名为scroller的类中:
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());
}
}
现在当我的一枚硬币的碰撞设置为真时,硬币会消失,但在碰撞设置为假后立即出现
答案 0 :(得分:2)
我强烈建议您重新构建代码。虽然起初可能会有些混乱,但以后会更容易。
假设您有一个名为coinList的
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());
}
}
虽然我并不完全熟悉您正在使用的游戏引擎,但我非常有信心实现这两项更改将为您提供所需的消失碰撞功能。我在浏览器中键入了所有这些代码,因此在使用代码之前可能需要进行一些修改。
每当你想要将新硬币添加到当前舞台/视图/游戏世界时,请确保在硬币列表上执行removeAll并将其手动添加回来,否则你最终会得到太多或者没有硬币。屏幕。
来源:Oracle Java 8 Javadoc Reference,很多Java标准库经验。