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

#
Needing help with the subject above i have tried this code: public void youWin() { if (score==5) { getWorld().addObject(new YouWin(), 400,300); Greenfoot.stop(); } } Had no errors while testing and couldnt get it to work. If you're needing any other code to see what the problem could be just ask thanks.
AdiBak AdiBak

2019/4/7

#
Make an actor subclass, called Score, that has a private int variable called 'score'. Add any methods that you want to use when adding to your score. Create a getter method for 'score' (possibly called getScore()) . In your World act method, reference and check your score by saying:
Score s = (Score)getObjects(Score.class).get(0);
if (s.getScore() == 5){
   addObject(new YouWin(), 400, 300);
   Greenfoot.stop();
}

shiney1884 shiney1884

2019/4/7

#
AdiBak wrote...
Make an actor subclass, called Score, that has a private int variable called 'score'. Add any methods that you want to use when adding to your score. Create a getter method for 'score' (possibly called getScore()) . In your World act method, reference and check your score by saying:
Score s = (Score)getObjects(Score.class).get(0);
if (s.getScore() == 5){
   addObject(new YouWin(), 400, 300);
   Greenfoot.stop();
}

ok i'll try that and get back to you
shiney1884 shiney1884

2019/4/7

#
AdiBak wrote...
Make an actor subclass, called Score, that has a private int variable called 'score'. Add any methods that you want to use when adding to your score. Create a getter method for 'score' (possibly called getScore()) . In your World act method, reference and check your score by saying:
Score s = (Score)getObjects(Score.class).get(0);
if (s.getScore() == 5){
   addObject(new YouWin(), 400, 300);
   Greenfoot.stop();
}

I have a counter subclass for the score if thats what you mean by creating a subclass called Score.Sorry if im misunderstanding im a beginner with this haha
AdiBak AdiBak

2019/4/7

#
shiney1884 wrote...
AdiBak wrote...
Make an actor subclass, called Score, that has a private int variable called 'score'. Add any methods that you want to use when adding to your score. Create a getter method for 'score' (possibly called getScore()) . In your World act method, reference and check your score by saying:
Score s = (Score)getObjects(Score.class).get(0);
if (s.getScore() == 5){
   addObject(new YouWin(), 400, 300);
   Greenfoot.stop();
}

I have a counter subclass for the score if thats what you mean by creating a subclass called Score.Sorry if im misunderstanding im a beginner with this haha
Yes, if the counter subclass does the above.
shiney1884 shiney1884

2019/4/7

#
AdiBak wrote...
shiney1884 wrote...
AdiBak wrote...
Make an actor subclass, called Score, that has a private int variable called 'score'. Add any methods that you want to use when adding to your score. Create a getter method for 'score' (possibly called getScore()) . In your World act method, reference and check your score by saying:
Score s = (Score)getObjects(Score.class).get(0);
if (s.getScore() == 5){
   addObject(new YouWin(), 400, 300);
   Greenfoot.stop();
}
I have a counter subclass for the score if thats what you mean by creating a subclass called Score.Sorry if im misunderstanding im a beginner with this haha
Yes, if the counter subclass does the above.
How do you create a getter method?
AdiBak AdiBak

2019/4/7

#
A getter is used to retrieve a value. In this case, to make a getter, create a method that has return type 'int' named 'getScore'. In the function, just return the score variable. It should look like this:
public int getScore(){
   return score;
}
shiney1884 shiney1884

2019/4/7

#
AdiBak wrote...
A getter is used to retrieve a value. In this case, to make a getter, create a method that has return type 'int' named 'getScore'. In the function, just return the score variable. It should look like this:
public int getScore(){
   return score;
}
public Counter getCounter()
    {
        return counter;
    }
I have this in my World
AdiBak AdiBak

2019/4/7

#
Yes, that will do.
shiney1884 shiney1884

2019/4/7

#
shiney1884 wrote...
AdiBak wrote...
A getter is used to retrieve a value. In this case, to make a getter, create a method that has return type 'int' named 'getScore'. In the function, just return the score variable. It should look like this:
public int getScore(){
   return score;
}
public Counter getCounter()
    {
        return counter;
    }
I have this in my World
And my scoreboard works properly,its just getting the game to display you win and and then end the game when it reaches 5
AdiBak AdiBak

2019/4/7

#
Wait, don't put that in your world, put that in your counter class.
shiney1884 shiney1884

2019/4/7

#
shiney1884 wrote...
shiney1884 wrote...
AdiBak wrote...
A getter is used to retrieve a value. In this case, to make a getter, create a method that has return type 'int' named 'getScore'. In the function, just return the score variable. It should look like this:
public int getScore(){
   return score;
}
So why is the code i had not working
public Counter getCounter()
    {
        return counter;
    }
I have this in my World
And my scoreboard works properly,its just getting the game to display you win and and then end the game when it reaches 5
shiney1884 shiney1884

2019/4/7

#
shiney1884 wrote...
shiney1884 wrote...
shiney1884 wrote...
AdiBak wrote...
A getter is used to retrieve a value. In this case, to make a getter, create a method that has return type 'int' named 'getScore'. In the function, just return the score variable. It should look like this:
public int getScore(){
   return score;
}
public Counter getCounter()
    {
        return counter;
    }
I have this in my World
And my scoreboard works properly,its just getting the game to display you win and and then end the game when it reaches 5
So why is the code i had not working?
AdiBak AdiBak

2019/4/7

#
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
shiney1884 shiney1884

2019/4/7

#
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++;
    }
    
}
There are more replies on the next page.
1
2