i have asteroids store in arraylist. i want to check collision between player and asteroids. if player get hit than i want to minus one life and move the asteroid backwards so it look like it bounced in to player.

right now this code is check for collision between player and asteroids and works fine.
the problem is that when asteroid collised with player than the collision say for lil while. bc asteroid moves slower than player and if player hold down up keep than it will keep collision. and the player lifes keep going down.

i want it so that if player bump into astroid than only one life should go down.

/**Collision between player and astroid **/
public void playerAsteroidCollision(ArrayList<asteroid> asteroid, player p)
{

    for(int i = 0; i < asteroid.size(); i++){
        asteroid asteroid = (asteroid)asteroid.get(i);
        if(player.getBounds().intersects(asteroid.getBounds())){
            player.setHit(true);              //player got hit
            player.setLife(player.getLife() -1);      //minus one life
            //change the direction of asteroid so it goes backwords
            asteroid.setDx(-asteroid.getDx()); 
            asteroid.setDy(-asteroid.getDy());
            }
        }
}/***End of playerAsteroidCollision Method/

You could have each asteroid keep track of whether it is touching the player, and only count it as a hit when the player touches an asteroid that it previously was not touching. That would let the player move through an asteroid, but if you want to prevent that you could also give the player's velocity to the asteroid.

In my opinion you are using the symbol asteroid for far too many distinct concepts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.