Search code examples
libgdx

collision detection and scoring is not working properly


I'm developing a game in libgdx. the character is jumping from piller to piller .I need to increment the score each time when the character is colliding on the piller, but i have written the collision detection between the piller and the character is on render method. So when the character is on piller the score is incrementing continously . Actully i need to increment the score when the character is on each piller (one time incrementation for one piller).so anybody can explain me how to set the flag for making the incrementation only once when the character collide with the object.

        public int getScore(){
           return 1; 
        }
        public void update(){
        if (character.overlaps(piller))
        { score+=getScore();

        }

Solution

  • I found the answer. when the character was colliding with the piller ,the score was incrementing continously . which is happend because the collision detection is checked by the libgdx is in the rate of 1/60 fps . so in each nano seconds it will detect the collision and it was incrementing the score. so for incrementing the score only once in one collision i have used the following small code and which works for me well.

          int count =0;
          if(pillerEctangle.overlaps(chaaracterRectangle)){
            count++;
          if (count % 60 == 0) {
    
                        Score += 1;
                        Syste.out.println(score+"incremented score");
                    } }
    

    // so here the score is increments once during a full collision