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

2014/9/20

Problem with resetting score to zero

1
2
3
DarkSoulDemon DarkSoulDemon

2014/9/20

#
I have a static int variable for my score so i can go up levels but now when i die and go onto the lost screen and reset and start over the score stays the same as when i died is any one able to help me it would be greatly appreciated.
danpost danpost

2014/9/20

#
The best way to do this would depend on several things: * how you maintain levels * your class structure * what transpires when your actor dies
Super_Hippo Super_Hippo

2014/9/20

#
Well, in general, you have to set the score to 0 in the constructor, because static variables are only created when the scenario is compiled, not when it is resetted. (I can't tell you the reason why it is made like this, but it is like that...) So it is not enough to have something like this outside the methods:
1
private/public static int score = 0;
Instead, you need to do this:
1
2
3
private/public static int score;
//in the constructor
score = 0;
DarkSoulDemon DarkSoulDemon

2014/9/21

#
Where would that go. Would it go in my level 1 or in my counter code or in the reset code
DarkSoulDemon DarkSoulDemon

2014/9/21

#
I go up levels by reaching a score i have set so to get to level 2 its ten points and level 2 is 20 points. The levels are sub classes from the one before. When my actor dies it come up with a screen that says you lost press N to reset you then press N and it brings you to the start screen and then you press R to go to level 1 but the score from when you died is still there and i want it to go back to 0 instead of the score that you die at
Super_Hippo Super_Hippo

2014/9/21

#
Put it in the constructor of the class in which you have declared the field. So when the world is created, the score will be set to 0.
DarkSoulDemon DarkSoulDemon

2014/9/21

#
It comes up with <identifier> expected at the point public static int score; //in the constructor score (here is where it says <identifier> expected) = 0;
Super_Hippo Super_Hippo

2014/9/21

#
You didn't put the last line in the constructor of the class. The constructor is the method that will be executed once when an object of this class is created. The constructor doesn't have a return type and the name is the name of the class. So if the name of the class is 'X', then the constructor looks like this:
1
2
3
4
public X()
{
    //...
}
DarkSoulDemon DarkSoulDemon

2014/9/21

#
oh okay
DarkSoulDemon DarkSoulDemon

2014/9/21

#
So where in here do i put it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
   
// i need to make an level varible
/**
 * Write a description of class Space here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Level1 extends Start
{
     Counter counter = new  Counter();
//      static int score;
     //int score1;
      
    public void act() 
    
      
    
     /**
     * Constructor for objects of class Space.
     *
     */
    public Level1()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(); 
        prepare();
        score = 0;
        reset();
       
     
    }
    public Counter getCounter()
     {
       return counter;
  
    }
     
    public void reset()
   {
        score =0;
   }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,14, 243);
 
        Rock rock =new Rock();
        addObject(rock, 673, 344);
 
        Rock rock2 = new Rock();
        addObject(rock2, 638, 55);
 
        Rock rock3 = new Rock();
        addObject(rock3, 672, 265);
 
        Rock rock4 = new Rock();
        addObject(rock4, 363, 225);
 
        Rock rock5 = new Rock();
        addObject(rock5, 697, 98);
 
        Rock rock6 = new Rock();
        addObject(rock6, 793, 269);
 
        Rock rock7 = new Rock();
        addObject(rock7, 832, 425);
 
        Rock rock8 = new Rock();
        addObject(rock8, 468, 348);
 
        Rock rock9 = new Rock();
        addObject(rock9, 523, 202);
 
        Rock rock10 = new Rock();
        addObject(rock10, 382, 69);
 
        Rock rock11 = new Rock();
        addObject(rock11, 380, 422);
 
        Rock rock12 = new Rock();
        addObject(rock12, 477, 115);
 
        Rock rock13 = new Rock();
        addObject(rock13, 477, 115);
 
        Rock rock14 = new Rock();
        addObject(rock14, 301, 90);
 
        Spawn spawn = new Spawn();
        addObject(spawn, 978, 486);
 
        addObject(counter, 47, 61);
         
    
    }
}
DarkSoulDemon DarkSoulDemon

2014/9/21

#
do i place it in my level 1 world or do i put it in my counter actor
danpost danpost

2014/9/21

#
First, you probably should not have each successive level subclass the previous one -- all your levels should be subclasses of the same class (whether it be World or a basic subclass of World). Next, you have line 13 commented out of the code. Uncomment that line. Finally, you do not need the 'reset' method. Remove it and the call to it within the constructor.
DarkSoulDemon DarkSoulDemon

2014/9/21

#
ive done that it still does not work
DarkSoulDemon DarkSoulDemon

2014/9/21

#
wheres the constructor and it has not worked
Super_Hippo Super_Hippo

2014/9/21

#
What is your 'Start' class? Do you also have a 'score' field there? Btw, rock12 and rock13 are on the same position. Does it change anything if you add a 'private' at the beginning of line 13?
There are more replies on the next page.
1
2
3