Reputation: 13
i am making an android game in eclipse, and in that game i have a coin and a player. i want my coin to dissappear after colliding with my player.
my code looks like this:
in my coin class:
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;
}
in another class called scroller:
public boolean collidesCoin(Player player){
return (coin1.collides(player)||coin2.collides(player)||coin3.collides(player));
}
and lastly in my gameworld class:
if (scroller.collidesCoin(player)){
addScore(1);
coin.reset(0);
countcoin=1;
}
it is in this last piece of code that i want the coin to disappear.
any suggestions how would do that?
Edit: i changed the drawing code of the coin so that it looks like this:
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());
}
}
now when the collision is set to true for one of my coins the coin dissappears but shows up right after the collision is set to false
Upvotes: 1
Views: 376
Reputation: 69
I strongly suggest restructuring your code. While it may be some confusion at first, it will make things much easier later on.
Assuming you have an ArrayList called 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
}
}
Because you have a Collection of Coins you can iterate through, this makes it so you don't have to manually write out the code for each individual coin.
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());
}
}
While I'm not completely familiar with the game engine you're using, I'm quite confident implementing these two changes will get you the disappear-on-collide functionality you want. I typed all this code in browser, so it will likely require some modification before working with your code.
Whenever you want to add new coins to the current stage/view/gameworld, make sure to do removeAll on the coin list and add them back in manually, otherwise you'll end up with either too many or no coins on the screen.
Sources: Oracle Java 8 Javadoc Reference, much Java Standard library experience.
Upvotes: 2