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

2014/9/5

"YOU WIN" screen help (score counter)

1
2
CiaranDenning CiaranDenning

2014/9/5

#
Hello. I recently uploaded my first Scenario (http://www.greenfoot.org/scenarios/12153) and I am trying to (in non-programming terms here) make it so that when all 15 hostages have been rescued, text appears similar to a game over screen. I have a score counter that functions so whenever the player "eats" the hostage = +1 score. This is my attempt which does not work :
1
2
3
4
5
6
if (score==15)
        
          VICTORY VICTORY = new VICTORY();
           addObject(VICTORY,getWidth()/2,getHeight()/2);
             Greenfoot.stop(); 
        }
Any help or nudges in the right direction would be greatly appreciated!
danpost danpost

2014/9/5

#
First, by convention, only final field names should be in all caps -- class names should begin with an uppercase and method names and fields should begin with a lowercase. Second, try to avoid using the exact name of the class as the name of an object of that class. So, instead of:
1
VICTORY VICTORY = new VICTORY();
use;
1
Victory victory = new Victory();
You could instead of 'score == 15' as the condition, use 'getObjects(Hostage.class).isEmpty()', which basically asks if all the hostages were eaten. Using a condition like this relies on the current state of the world and not on the value of a field (which may not have the correct value).
CiaranDenning CiaranDenning

2014/9/5

#
Thank you danpost for the first 2 lessons, but the problem with the solution you gave me, and I apologise as I should have been more specific, is that there isn't a singular Hostage.class, but rather 4 different classes. How can I work around this now? Do I repeat it for all 4? I can provide any sc you need :)
danpost danpost

2014/9/5

#
Well, you could do this:
1
2
3
4
if (getObjects(HostageA.class).isEmpty() &&
    getObjects(HostageB.class).isEmpty() &&
    getObjects(HostageC.class).isEmpty() &&
    getObjects(HostageD.class).isEmpty() )
However, it might be better to create a simple Hostage class that all four classes could extend and use what I gave above (maybe not better for you, but better for how your scenario will run -- it will only have to iterate through the objects in the world once instead of four times each act).
danpost danpost

2014/9/5

#
Either way, you may still have a problem with your 'score' value. Run the scenario picking up a hostage; then 'Pause' the scenario. Right click on the background of the world and select 'Inspect' from the list of options in the pop-up. Find the 'score' field and report back what value it has
CiaranDenning CiaranDenning

2014/9/5

#
Yes the score is working, it is a static field
danpost danpost

2014/9/5

#
NVM, just saw your last post.
danpost danpost

2014/9/5

#
Do you set the 'score' field to zero in your world constructor?
CiaranDenning CiaranDenning

2014/9/5

#
does "static int score" do the job or?
danpost danpost

2014/9/5

#
CiaranDenning wrote...
does "static int score" do the job or?
If you are referring to doing the job of setting the field to zero, then only initially. That is, when your project is compiled, the value of score will be initialized to zero; however, during successive resets, the value will not be reset to zero. Static fields are not reset to default values during resets. Therefore, you must programmatically set them to zero in your world constructor unless you do want their values to be retained.
CiaranDenning CiaranDenning

2014/9/5

#
I changed my code so that the condition used is "getObjects(Hostage.class).isEmpty()." Can you point me in the right direction to fix this if we know that my score is working (when I inspected the background after picking up the corresponding amount of "hostages")
danpost danpost

2014/9/5

#
Once your score is fixed, you can go back to the original condition:
1
if (score == 15)
But, you should be absolutely positive that the value of the score will be correct at all times.
CiaranDenning CiaranDenning

2014/9/5

#
It still does not work :(
danpost danpost

2014/9/5

#
Does it work the first time after compiling the project? or is your 'score' field not containing the correct values? Saying, 'it still does not work' does not give any helpful information to assist you with. How is it not working? What behavior are you currently getting that you should not be getting? What details can you provide that may help in solving your dilemma?
KiwiTitan KiwiTitan

2015/9/21

#
lololol
There are more replies on the next page.
1
2