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

2019/4/7

Need help with getting a you win logo to display and the game to end when the score reaches 5

1
2
shiney1884 shiney1884

2019/4/7

#
shiney1884 wrote...
AdiBak wrote...
Just to review: In your Counter class: // initialize a variable named 'score' // create a getter method like the one mentioned above In your world class: // reference and check the score like mentioned above
import greenfoot.*;  

public class Counter extends Actor
{
    int score = 0;
    
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color .BLACK, Color.GREEN));
    }    
    
    public void addScore()
    {
      score++;
    }
    
}
this is what i have in counter class
AdiBak AdiBak

2019/4/7

#
Add the getter method in the Counter class.
shiney1884 shiney1884

2019/4/7

#
AdiBak wrote...
Add the getter method in the Counter class.
public int getScore(){
   return score;
     }
ok done this
shiney1884 shiney1884

2019/4/7

#
shiney1884 wrote...
AdiBak wrote...
Add the getter method in the Counter class.
public int getScore(){
   return score;
     }
ok done this
correct?
AdiBak AdiBak

2019/4/7

#
Yes, it's fine. Now, in the world class, do the following: Counter s = (Counter)getObjects(Counter.class).get(0); if (s.getScore() == 5){ addObject(new YouWin(), 400, 300); Greenfoot.stop(); }
shiney1884 shiney1884

2019/4/7

#
shiney1884 wrote...
shiney1884 wrote...
AdiBak wrote...
Add the getter method in the Counter class.
public int getScore(){
   return score;
     }
ok done this
correct?
import greenfoot.*;  


public class River extends World
{
    Counter counter = new Counter();
    private int score=0;
    public void act()
    {
        SharkEnters();
        
       
      }

    public River()
    {    
        super(800, 600, 1); 
        addObject(counter, 100, 40);
        addObject(new Tent(),400,510);
        addObject(new Person(),400,510);
        addObject(new Food(),100,100);
        addObject(new Food(),250,100);
        addObject(new Food(),400,100);
        addObject(new Food(),550,100);
        addObject(new Food(),700,100);
       
    }

    public void SharkEnters()
    {
        if( Greenfoot.getRandomNumber(100)<1)
        {
            addObject(new Shark(),25,300);
        }

    }
    
    public Counter getCounter()
    {
        return counter;
    }

    
}
and here's my world class
AdiBak AdiBak

2019/4/7

#
First, remove the getCounter method. Second, add an act method and put the code I mentioned above in it.
shiney1884 shiney1884

2019/4/7

#
AdiBak wrote...
Yes, it's fine. Now, in the world class, do the following: Counter s = (Counter)getObjects(Counter.class).get(0); if (s.getScore() == 5){ addObject(new YouWin(), 400, 300); Greenfoot.stop(); }
That works thanks so much for your help
Super_Hippo Super_Hippo

2019/4/8

#
You could just use this as your Counter class. Then you only change the image when needed and only check for score=5 when it is needed (=when it changed):
import greenfoot.*;  
 
public class Counter extends Actor
{
    private int score = 0;
     
    public Counter() 
    {
        updateImage();
    }    
     
    public void addScore()
    {
        score++;
        updateImage();
        if (score == 5)
        {
            getWorld().addObject(new YouWin(), getWorld().getWidth()/2, getWorld().getHeight()/2);
            Greenfoot.stop();
        }
    }
     
    private void updateImage()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.BLACK, Color.GREEN));
    }
}
You need to login to post a reply.
1
2