ProggrammingNoob
ProggrammingNoob

Reputation: 13

making object dissappear after collision! java

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

Answers (1)

MattMerr47
MattMerr47

Reputation: 69

I strongly suggest restructuring your code. While it may be some confusion at first, it will make things much easier later on.

  1. My first suggestion is to keep the coins in a dynamically-sized Collection such as a List or a Map.
    • If the Coin class holds a reference to it's ID a List is probably the easiest choice, but a Map from ID to Coin is an equally valid design choice. (See the javadocs for ArrayList and HashMap for Collection implementation-specific details.)
    • For the purpose of continued discussion, let's assume you choose to put all your Coin objects in a List.
  2. Don't manually write the code for each individual coin.
    • If you later decide you want to add another coin, you'll have to add more conditional statements. When you have n coins, you'll have 2^n conditional statements for checking whether or not to draw a coin. This gets very big very quickly and is quite prone to error.
    • If you have all the coins in an Iterable data structure, which all Java classes extending Collection list List or Map are, you can use a for-each loop or an iterator.

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

Related Questions