This site requires JavaScript, please enable it in your browser!
Greenfoot back
JWK3986
JWK3986 wrote ...

2016/11/7

Trying to display score on Goldfish actor wherever it is located

JWK3986 JWK3986

2016/11/7

#
I have been able to successfully get the score to pop up at the Goldfish's location but it then stays there and another "Score: " with whatever number representing the change in score pops up at the next location where the Goldfish eats another Puffer for a point. How can I make the score be displayed only where the Goldfish is?
 /**
     * Method to  make Goldfish eat touching puffer and score a point
     */
    
   public void eatPuffer()
   {
       if(isTouching(Puffer.class) == true)
       {
          removeTouching(Puffer.class);
          Greenfoot.playSound("crunch.wav");
          score = score + 1;
          getWorld().showText("Score: " + score, getX(), getY());
          
          
          
          
           
        }
       if(score==50)
       {
           GreenfootSound sound1 = new GreenfootSound("victory.mp3");
            sound1.play();
            getWorld().addObject(new Victory(), 390, 180);
            while(sound1.isPlaying()) {} // wait til sound1 finishes playing
            Greenfoot.playSound("you win.mp3");
            Greenfoot.stop();
           
           
           
        }
       
       
       
    }
danpost danpost

2016/11/7

#
You may have to save the location where you originally had shown the score. That way, you can remove it using 'null' for the string and update the location when showing the new score:
// add instance fields
private int scoreX, scoreY;

// when scoring
getWorld().showText(null, scoreX, scoreY);
score = score + 1;
getWorld().showText("Score: " + score, getX(), getY());
You need to login to post a reply.