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

2013/1/8

Adding a scoreboard

1
2
LonelyCreeper LonelyCreeper

2013/1/8

#
In my game, I need to make it so that when an actor eats another actor, the score is recorded in a scoreboard. How do I create the scoreboard and allow it to record scores?
Gevater_Tod4711 Gevater_Tod4711

2013/1/8

#
The easyest way to use a scorecounter is to import the class counter which is a greenfoot standard class. In your scenario click on edit, import class, and import the class counter.
LonelyCreeper LonelyCreeper

2013/1/9

#
How do I write the code to make it increase the score?
Gevater_Tod4711 Gevater_Tod4711

2013/1/9

#
There is a method add(int score) in the class. If you call this method like this:
1
theReferenceToTheObject.add(1);
It'll count up one scorepoint.
LonelyCreeper LonelyCreeper

2013/1/9

#
When I put tht in it says non-static method add(int) cannot be referenced from a static context. Why does that mean and what should I do?
Gevater_Tod4711 Gevater_Tod4711

2013/1/9

#
That means you are trying to add the score like this:
1
Counter.add(1);
but you cant call the method add with the name of the counter. You need a reference. You probably got only one counter object in your world so it's easy to get the reference:
1
2
3
4
5
public void addScore(int sceor) {
    if (!getWorld().getObjects(Counter.class).isEmpty()) {
        getWorld().getObjects(Counter.class).get(0).add(score);
    }
}
If you got more than one counter objects this will not work like you want it to but you probably got only one.
danpost danpost

2013/1/9

#
Line 3 in Gevater_Tod4711's code should be
1
((Counter) getWorld().getObjects(Counter.class).get(0)).add(score);
It will not compile without casting the Counter object because the 'add' method will not be found in the 'Actor' class (needs to be found in the 'Counter' class).
LonelyCreeper LonelyCreeper

2013/1/9

#
It says it isn't a statement.
Gevater_Tod4711 Gevater_Tod4711

2013/1/9

#
Could you post your code?
LonelyCreeper LonelyCreeper

2013/1/10

#
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
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
 
public class Crab extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int wormsEaten;
    private CrabWorld crabWorld = null;
    private ScoreBoard scoreBoard = null;
     
    public Crab()
    {
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage(image1);
        wormsEaten = 0;
    }
     
    /**act- includes all methods that allow the crab to perform all actions needed in the game.*/
    public void act()
    {
        checkKeypress();
        lookForWorm();
        switchImage();
    }
     
    /**selectImage- allows the crab to select the images that it will need to change between for the switchImage() method.*/
    public void selectImage()
    {
        if (getImage() == image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
     
    /**switchImage- allows the crab to change between images when it moves so that it looks like it's moving.*/
    public void switchImage()
    {
        if (Greenfoot.isKeyDown("up"))
        {
        selectImage();
        }
        if (Greenfoot.isKeyDown("down"))
        {
        selectImage();
        }
        if (Greenfoot.isKeyDown("left"))
        {
        selectImage();
        }
        if (Greenfoot.isKeyDown("right"))
        {
        selectImage();
        }
    }
     
    /**checkKeypress- allows the crab to move and turn in different directions according to the arrows being pressed on the keyboard.*/
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-6);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(6);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            move(7);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            move(-7);
        }
    }
 
    /**lookForWorm- allows the crab to eat the worms and add score and wormcounts accordingly.*/
    public void lookForWorm()
    {
        CrabWorld datWorld= (CrabWorld) getWorld();
         
        if (canSee(Worm.class))
        {
            eat(Worm.class);
            Greenfoot.playSound("slurp.wav");
            addScore();
        }
    }
     
    public void addScore()
    {
        if (!getWorld().getObjects(Counter.class).isEmpty())
        {
            ((Counter) getWorld().getObjects(Counter.class).get(0))add(1);
        }
    }
     
    /**setLocation- This code is here so that the Rocks and Trees act as obstacles and the character can't walk through them.*/
    public void setLocation(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x,y);
        if(!getIntersectingObjects(Rock.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
        if(!getIntersectingObjects(Tree.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }
     
    }
danpost danpost

2013/1/10

#
Line 99 is missing a 'dot' (.) before 'add'.
LonelyCreeper LonelyCreeper

2013/1/10

#
ok it works! thank you!
nmichael221 nmichael221

2014/12/3

#
so I did the same thing, but it says it can't find the symbol for add. help?
danpost danpost

2014/12/3

#
There seems to be a dot, '.' missing before the 'add(1)' on line 99. Make sure you have one there.
beliuga beliuga

2015/1/10

#
I have two players in my game; but I can't import another counter?
There are more replies on the next page.
1
2