I am developing a game. When my main character intersects with a star(point), I should get 1 point. I used Intersector.overlaps but when both are intersected, score is increasing continuosly until they don't touch each other. How can i fix it? I initialized int score = 0, and here my collision code,
(starAvailable = I am removing star from screen after collision. Not an important thing for scoring.)
if (Intersector.overlaps(charCircle,starCircles1[i])) {
starAvailable1=false;
score++;
}
For example, when i intersect with a star, score is becoming like this
(starAvailable = I am removing star from screen after collision. Not an important thing for scoring.)
It is actually important. The easiest and most efficient way you have to do this is to remove (or move away out of the visible field, disable... whatever you are doing to save memory) the star straight before or after changing the score, so make sure you will not trigger further collisions. There is not enough code to know how to do it in your project, but something like...
if (Intersector.overlaps(charCircle,starCircles1[i])) {
//starAvailable1=false;
removeStarFromField(); // we disable the collider, stop rendering,
// or remove the star
score++;
}
As a less effective alternative, but which may be more accessible if you have not a lot of experiece with LibGdx, and if your star has some sort of unique identifier or you can add it to it, is you could store which stars you collided with (or if you already collided with one specific star, if you are caching them). Once you collide with the star, you cache that identifier. You then should check the id fromt he star you hit against your cache, and if it's there, you shouldn't increase the score, something akin to:
// somewhere else...
Set<String> identifiersFromAlreadyHitStars = new HashSet<>();
....
// then your actual collision detection method
if (Intersector.overlaps(charCircle,starCircles1[i])) {
if(!identifiersFromAlreadyHitStars.contains(hitStar.identifier){
score++;
identifiersFromAlreadyHitStars.add(hitStar.identifier)
}
}