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

2015/1/19

Counter!

Pe1per Pe1per

2015/1/19

#
In my game I've created a Counter in a world and I placed the counter also in the Game Over world. My problem is I can't get the score from the world in the gameOver world, so the counter starts every time at zero! How can I save the score in the main world and show it in the GameOver world? :)
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
public class Counter extends Actor
{
  public int score = 0;
  private int time = 0;
   
  public int getScore(){
        return score;
    }
     
  public void act()
  {
      addScore(); // erhöht den Counter
    }
     
  public Counter()
  {
      score = 0;
      setImage(new GreenfootImage(400,120)); //Größe für Bild des Counters
      update();
    }
     
  public void addScore()
  {
      time++; // Zeit +1 bis Counter um 1 hoch geht
      if (time == 62){ // wenn Zeit bei 62 ist geht Counter um 1 hoch
      score++;
      update(); // updatet das Bild des Counters
      time = 0; // stellt Zeit bis zum näachsten erhöhen des Counters auf null
    }
  }
   
  public void update() // erneuert das Bild nach jedem dazuzählen
  {
      GreenfootImage img = getImage();
      img.clear(); // löscht Bild
      img.setColor(Color.BLACK); // setzt Farbe der Schrift auf Schwarz
      img.drawString("Score: " + score, 12, 60); // addiert den Score
  }
}
The hud isn't important it's only the picture behind the Counter!
1
2
3
4
5
6
7
8
9
10
11
12
13
public GameOver()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1600, 790, 1);
          
        Hud hud = new Hud();
        addObject ( hud, 86, 747);
         
        Counter counter = new Counter();
        addObject ( counter, 210, 752);
                       
        prepare();
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public level1()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1600, 800, 1);
         
        Hud hud = new Hud();
        addObject(hud, 93, 753);
        hud.setLocation(86, 747);
         
        Counter counter = new Counter();
        addObject ( counter, 210, 752);
         
        tank tank = new tank();
        addObject(tank, 810, 494);
         
        prepare();
    }
danpost danpost

2015/1/19

#
You need to show the code where the GameOver world is created (where 'new GameOver()' is used). Also, how you reference the Counter object in the class that code is in.
Pe1per Pe1per

2015/1/19

#
1
2
3
4
5
6
if(Zombie != null && lives == 0 && LifeTime < 10){ // wenn er kollidiert ist das Spiel vorbei
           Greenfoot.setWorld(new GameOver());
           Greenfoot.playSound("hit1.mp3"); // Endmusik
           Greenfoot.playSound("gameover.mp3"); // Endmusik
           DeathCount = 1; // wird benötigt um Abfrage zu beenden ob Panzer mit Munition kolidiert
        }
thats the code.. I don't have writen anything to connect the counter with the gameOver world because I've no Idea what I should do :)
danpost danpost

2015/1/19

#
Although there is nothing in the last post that tells me outright that the code is in an Actor subclass, there are hints that suggest it is. Post the code in the same class as this last bit of code that adds to the score counter (I did ask for how you reference the Counter object in that class -- maybe I was a bit unclear as to what I meant by that).
Pe1per Pe1per

2015/1/19

#
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
public class tank extends Actor
{
    private int Keycounter = 0; //zählt wie viele Keys gleichzeitig gedrückt werden
    private int ammo = 20; // Munition von Panzer
    private int DeathCount = 0; // Anzeige für Tod oder Leben
    private int LifeTime = 0;
    private int lives = 3;
    boolean LifeLost = false;
     
    public void act()
    {
        checkKeys();
        checkColision();
        if (DeathCount == 0){
          checkMunition();
        }
    }   
     
    private void checkKeys() // Steuerung Panzer
    {
        /** Bewegung Panzer links, rechts, oben und unten, wenn key gedrückt ist und keine
         *  Wand da ist. (Rotation anstatt turn für feste Richtungen)
         */
        Keycounter = 0; // setzt Zähler nach jedem drücken wieder auf 0
         
        if(Greenfoot.isKeyDown("a") && getOneObjectAtOffset(-50, 45, brickwall.class) == null && getOneObjectAtOffset(-50, -45, brickwall.class) == null && Keycounter < 1 && getOneObjectAtOffset(-50, 45, ironWall.class) == null && getOneObjectAtOffset(-50, -45, ironWall.class) == null){
            setRotation(0); // Richtung
            move(-4); //Schnelligkeit
            Keycounter++; // zählt auf eins hoch und blockiert die anderen Tasten
        }
         
        if(Greenfoot.isKeyDown("d") && getOneObjectAtOffset(50, 45, brickwall.class) == null && getOneObjectAtOffset(50, -45, brickwall.class) == null && Keycounter < 1 && getOneObjectAtOffset(50, 45, ironWall.class) == null && getOneObjectAtOffset(50, -45, ironWall.class) == null){
            setRotation(180);
            move(-4);
            Keycounter++;
        }
         
        if(Greenfoot.isKeyDown("s") && getOneObjectAtOffset(45, 50, brickwall.class) == null && getOneObjectAtOffset(-45, 50, brickwall.class) == null && Keycounter < 1 && getOneObjectAtOffset(45, 50, ironWall.class) == null && getOneObjectAtOffset(-45, 50, ironWall.class) == null){
            setRotation(270);
            move(-4);
            Keycounter++;
        }
             
        if(Greenfoot.isKeyDown("w") && getOneObjectAtOffset(45, -50, brickwall.class) == null && getOneObjectAtOffset(-45, -50, brickwall.class) == null && Keycounter < 1 && getOneObjectAtOffset(45, -50, ironWall.class) == null && getOneObjectAtOffset(-45, -50, ironWall.class) == null){
            setRotation(90);
            move(-4);
            Keycounter++;
        }
         
        if("space".equals(Greenfoot.getKey()) && ammo > 0){ //wenn leertaste gedrückt feuert er
           fire();
        }
    }
     
    private void checkColision() // überprüft ob Panzer mit Zombie kollidiert
    {
        LifeTime--;
        Actor Zombie = getOneIntersectingObject(Zombie.class);
        Actor Leben = getOneIntersectingObject(Leben.class);
        Actor Ghast = getOneIntersectingObject(Ghast.class);
        Actor schuss2 = getOneIntersectingObject(schuss2.class);
         
        if(Zombie != null){
        if(Zombie != null && lives > 0 && LifeTime < 10){
           LifeTime = 40;
           getWorld().removeObject(Zombie);
           if ( lives == 3){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("2Herzen.png");
             leben.getImage().scale( 200, 80);
           }
           if ( lives == 2){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("1Herzen.png");
             leben.getImage().scale( 200, 80);
           }
           if ( lives == 1){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("tankHurt.png");
             leben.getImage().scale( 200, 80);
           }
           Greenfoot.playSound("hit1.mp3");
           lives--;
        }
        if(Zombie != null && lives == 0 && LifeTime < 10){ // wenn er kollidiert ist das Spiel vorbei
           Greenfoot.setWorld(new GameOver());
           Greenfoot.playSound("hit1.mp3"); // Endmusik
           Greenfoot.playSound("gameover.mp3"); // Endmusik
           DeathCount = 1; // wird benötigt um Abfrage zu beenden ob Panzer mit Munition kolidiert
        }
       }
        
       if(schuss2 != null){
        if(schuss2 != null && lives > 0 && LifeTime < 10){
           LifeTime = 40;
           getWorld().removeObject(schuss2);
           if ( lives == 3){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("2Herzen.png");
             leben.getImage().scale( 200, 80);
           }
           if ( lives == 2){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("1Herzen.png");
             leben.getImage().scale( 200, 80);
           }
           if ( lives == 1){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("tankHurt.png");
             leben.getImage().scale( 200, 80);
           }
           Greenfoot.playSound("hit1.mp3");
           lives--;
        }
        if(schuss2 != null && lives == 0 && LifeTime < 10){ // wenn er kollidiert ist das Spiel vorbei
           Greenfoot.setWorld(new GameOver());
           Greenfoot.playSound("hit1.mp3"); // Endmusik
           Greenfoot.playSound("gameover.mp3"); // Endmusik
           DeathCount = 1; // wird benötigt um Abfrage zu beenden ob Panzer mit Munition kolidiert
        }  
       }
        
       if(Ghast != null){
        if(Ghast != null && lives > 0 && LifeTime < 10){
           LifeTime = 40;
           getWorld().removeObject(Ghast);
           if ( lives == 3){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("2Herzen.png");
             leben.getImage().scale( 200, 80);
           }
           if ( lives == 2){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("1Herzen.png");
             leben.getImage().scale( 200, 80);
           }
           if ( lives == 1){
             Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
             leben.setImage("tankHurt.png");
             leben.getImage().scale( 200, 80);
           }
           Greenfoot.playSound("hit1.mp3");
           lives--;
        }
        if(Ghast != null && lives == 0 && LifeTime < 10){ // wenn er kollidiert ist das Spiel vorbei
           Greenfoot.setWorld(new GameOver());
           Greenfoot.playSound("hit1.mp3"); // Endmusik
           Greenfoot.playSound("gameover.mp3"); // Endmusik
           DeathCount = 1; // wird benötigt um Abfrage zu beenden ob Panzer mit Munition kolidiert
        }
       }
    }
     
    private void fire() //erstellt neuen schuss
    {
        schuss1 schuss1 = new schuss1();
        getWorld().addObject(schuss1, getX(), getY());
        schuss1.setRotation(getRotation()); // Schuss bekommt Richtung von Panzer
        schuss1.move(-78); // Schuss erscheint vor dem Panzerrohr und nich in der mitte
        Greenfoot.playSound("fire.wav"); // Geräusch des Schusses
        ammo--; //Eine Munition wird abgezogen
    }
     
    private void checkMunition()
    {
        Actor Munition = getOneIntersectingObject(Munition.class);
        if (Greenfoot.isKeyDown("e") && Munition != null){ // wenn man bei einem Munitionspacket steht und e drückt füllt sich die Munition auf!
            ammo = 25; // Man hat wieder 20 Schüsse
            getWorld().removeObject(Munition); // Munitionspacket wird aus der Welt entfernt
        }
    }
     
    public tank()
    {
        getImage().scale(100,65); //Größe des Panzers (X,Y)
    }
}
Ok thats the hole code of my tank class which change the world from level1 to GameOver...
danpost danpost

2015/1/19

#
Sorry. That will not help. Normally scoring is done in the class of the main actor. It must be in one of your other Actor subclasses. Alright, replace line 86 with this:
1
2
3
4
5
6
level1 level = (level1)getWorld(); //gets current level1 world
Counter counter = (Counter)level.getObjects(Counter.class).get(0); // gets counter from current world
GameOver go = new GameOver(); // creates game over world
Counter goCounter = (Counter)go.getObjects(Counter.class).get(0); // gets counter in game over world
goCounter.score = counter.getScore(); // sets game over counter value to score of current world
goCounter.update(); // updates the image of the game over counter
Pe1per Pe1per

2015/1/19

#
Ok I've changed it but now every time i lost all lives and hit a Zombie, the sound is played a hundred of times and the world doesn't change why?
danpost danpost

2015/1/19

#
Sorry. I forgot the final line of code:
1
Greenfoot.setWorld(go); // set the game over world active
Pe1per Pe1per

2015/1/19

#
oh ok :) can you explain me the importance of go? I don't understand it...
danpost danpost

2015/1/19

#
'go' is just a variable name, short for Game Over. It holds a reference to the game over world created so we can call methods on it (like accessing its counter). I could just as easily have called it 'gameOver' or 'gameover'.
Pe1per Pe1per

2015/1/19

#
ok thank you :) and now I've just a last question: How can I stop the counter when the world change to GameOver?
danpost danpost

2015/1/19

#
Wow. I did not realize that the counting was continuous (I see that now, as I look back at the Counter class code). That is not too difficult to deal with, however. Change the act method in the Counter class to this:
1
2
3
4
5
public void act()
{
    if (getWorld() instanceof GameOver) return;
    addScore(); // erhöht den Counter
}
Pe1per Pe1per

2015/1/19

#
Thank you it works ;) can you explain what instanceof mean?
Super_Hippo Super_Hippo

2015/1/19

#
An object is an instance of a class. So it checks if the current world is an object of the class GameOver.
Pe1per Pe1per

2015/1/19

#
thank you ;)
You need to login to post a reply.