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

2019/12/7

Score counter isn't working

annika.g annika.g

2019/12/7

#
So I followed a youtube tutorial on how to create a score counter, the counters there and everything but it won't count anything. If the actor, Pacman, eats a coin the score should go up by one and if he eats a Kirsche (cherrie) the score should go up by 2. But it just doesn't go up. Can somebody help me? Code of the counter1 class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;
import java.awt.Color;
public class Counter1 extends Actor
{
  int score = 0;
  public Counter1()
  {
    setImage(new GreenfootImage("Score: " + score, 24, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
  }
  public void addScore()
  {
       score++;
       updateImage();
  }
  public void updateImage()
  {
        setImage(new GreenfootImage("Score: " + score, 24, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
  }
  
}
code of the actor, who eats the coins/Kirschen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Actor Kirsche = getOneIntersectingObject (Kirsche.class);   
            if(Kirsche!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Kirsche);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.addScore();
              counter1.updateImage();
            }
            Actor Coin = getOneIntersectingObject (Coin.class);
            if(Coin!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Coin);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.updateImage();
The world code:
1
2
3
4
5
public class RoboterWelt extends World
    Counter1 counter1 = new Counter1();
    private static int zellenGroesse = 20;
    private int score = 0;}
1
2
3
4
public Counter1 getCounter1()
    {
        return counter1;
    }
I didn't put anythinf in the coin or Kirsche class, maybe that's the mistake? I don't know, please help me.
danpost danpost

2019/12/7

#
Remove:
1
import java.awt.Color;
from any and all classes. Then, you can drop 'greenfoot.' from in front of all 'Color'. Remove:
1
private int score;
from your RoboterWelt class. Your Counter1 object holds the score value. Show your entire RoboterWelt class for review.
annika.g annika.g

2019/12/7

#
Alright, I did that but it still doesn't work. I just checked and the score counter increases its score when Pacman hits a ghost for the first time. The other times he hits a ghost it doesn't go up. Anyway, heres the code for the RoboterWelt:
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Die einzigen aktiven Akteure in der Roboterwelt sind die Roboter.
 * Die Welt besteht aus 26 * 34 Feldern.
 */
 
public class RoboterWelt extends World
    Counter1 counter1 = new Counter1();
    private static int zellenGroesse = 20;
     
    HealthBar healthbar = new HealthBar();
 
 
    /**
     * Erschaffe eine Welt mit 26 * 34 Zellen.
     */
     
    public RoboterWelt()
    {
        super(26, 34, zellenGroesse);
        GreenfootImage bg = new GreenfootImage ("Black_from_a_camera.jpg");
        
         
        setPaintOrder(Schraube.class, Akku.class,  Roboter.class, Wand.class);
        Greenfoot.setSpeed(35);  
        prepare();
    }
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
    public Counter1 getCounter1()
    {
        return counter1;
    }
    public void act()
    {if(healthbar.health<0)
                    {
                         
                        Greenfoot.setWorld(new GameOver());
                    }}
                     
    private void prepare()
    {   addObject (healthbar, 21, 32);
        addObejct (counter1, 2, 32);
danpost danpost

2019/12/7

#
annika.g wrote...
the score counter increases its score when Pacman hits a ghost for the first time. The other times he hits a ghost it doesn't go up.
Please show Pacman codes.
annika.g annika.g

2019/12/7

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int score;
     public void KirscheAufnehmen()
    {
            Kirsche aktKirsche = (Kirsche)this.getOneObjectAtOffset(0, 0, Kirsche.class);
            if(aktKirsche != null)
            {
                this.getWorld().removeObject(aktKirsche);
                score ++;
                score++;
            }
        }
    public void CoinAufnehmen()
    {
            Coin aktCoin = (Coin)this.getOneObjectAtOffset(0, 0, Coin.class);
            if(aktCoin != null)
            {
                this.getWorld().removeObject(aktCoin);
                score ++;
            }
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
public boolean KirscheAufFeld()
    {
        if (this.getOneObjectAtOffset(0, 0, Kirsche.class)!= null)
        {
           return true;    
        }
        else
        {
            return false;  
        }
    }
     public boolean CoinAufFeld()
    {
        if (this.getOneObjectAtOffset(0, 0, Coin.class)!= null)
         
        {
           return true;    
        }
        else
        {
            return false;  
        }
    }
     public boolean BlauerGeistAufFeld ()
    {
        if (this.getOneObjectAtOffset(0, 0, BlauerGeist.class)!= null)
        {
           return true;    
        }
        else
        {
            return false;  
        }
    }
  [code]public void act()
    {
        health=3;
         
            if(Greenfoot.isKeyDown("up"))
            {setRotation(Direction.UP);
                bewegen();
            }
            else  
            if(Greenfoot.isKeyDown("down"))
            {setRotation(Direction.DOWN);
                bewegen();
            }
            else 
            if(Greenfoot.isKeyDown("left"))
            {setRotation(Direction.LEFT);
                bewegen();
            }
            else
            if(Greenfoot.isKeyDown("right"))
            {setRotation(Direction.RIGHT);
                bewegen();
            }
            if (KirscheAufFeld())
                {
                    KirscheAufnehmen();
                    
                }
            
            Actor Kirsche = getOneIntersectingObject (Kirsche.class);   
            if(Kirsche!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Kirsche);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.addScore();
              counter1.updateImage();
            }
            Actor Coin = getOneIntersectingObject (Coin.class);
            if(Coin!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Coin);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.updateImage();
            }
    }
        public void hitBlauerGeist()
        {
                Actor BlauerGeist = getOneIntersectingObject(BlauerGeist.class);
            if (BlauerGeist!=null)
            {
                World myWorld = getWorld();
                RoboterWelt roboterwelt =  (RoboterWelt)myWorld;
                HealthBar healthbar = roboterwelt.getHealthBar();
                if (touchingBlauerGeist == true)
                {
                    healthbar.loseHealth();
                    touchingBlauerGeist = true;
                     
                    if(healthbar.health<0)
                    {
                        myWorld.removeObject(this);
                        Greenfoot.setWorld(new GameOver());
                    }
                }
                
             
        }
        else
        { touchingBlauerGeist = false;
            
        }
danpost danpost

2019/12/7

#
I do not see anywhere where the score is upped when hitting a ghost. Also, I do not see from where the hitBlauerGeist method is being called. Q: in what class is the 19-line code set just given above? and why is there a score field there when you already have one in the Counter1 class?
You need to login to post a reply.