1
\$\begingroup\$

I have gold stars in my game that give score to game hero if the hero overlap with the star. What I want is to remove star once it overlap with hero object. I give score by checking overlap with this code

public void scorecount(Hero myhero){

            if (!star.isScored() && Intersector.overlaps(myhero.getBoundingBalloon(), star.getStarCircle())) 
    {

                addScore(1); // give one plus always
  star.setScored(true);


AssetLoader.coin.play();

}

Now once I give score and setScore true , i want to remove star. How can I do that? I tried for setvisible method but I dont know how to use it. Because just Calling star.setVisible() ask to create this method in star class. then in star class I dont know what should be given inside this method body.

\$\endgroup\$
1
\$\begingroup\$

You could use an ArrayList to store all your stars in. When collision is detected you remove star from array so the star is no longer updated.

So :

create :

ArrayList <Star> stars= new ArrayList<Star>();

add the stars :

stars.add (new Star (param1, param2));

update :

    for (int i = 0; i < stars.size(); i++) {
       star.get(i).update (param1, param2);
    }

        for (int i = 0; i < stars.size(); i++) {
          if (player.overlaps(stars.get(i)) { 
              // remove star from arraylist
              stars.remove(i);
              // another approach would be not to remove the star, but to respawn it
              // on a different position; it depends what your game logic require
              stars.get(i).respawn (param1, param2);
         }
            }
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.