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

2013/10/17

"Cannot find symbol- variable counter"

quicklearner quicklearner

2013/10/17

#
Okay I have been following "18km" greenfoot tutorials but I have slightley changed them to a space rocket theme. I'm on video 16 and I'm trying to add a counter so that every time my rocket saves someone(eats them) it adds to the counter.But I keep getting this error and I cant think how to fix it, it comes up when I enter "counter" between parentheses "brackets" on my rocket location thing,on my world class "space." I have done everything else in the video the counter.class etc. but I really cant work out how to fix it. I'm kind of a noob to this so please tell me what code you would need to see to fix it or whatever, thanks.
Gevater_Tod4711 Gevater_Tod4711

2013/10/17

#
Could you post the code of your world class? Or the class where the error occours.
quicklearner quicklearner

2013/10/17

#
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
{
 
    /**
     * Constructor for objects of class Space.
     *
     */
    public Space()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 500, 1);
 
        prepare();
    }
 
    /**
     * 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(counter);
        addObject(rocket, 22, 215);
        Rock rock = new Rock();
        addObject(rock, 132, 96);
        Rock rock2 = new Rock();
        addObject(rock2, 266, 394);
        Rock rock3 = new Rock();
        addObject(rock3, 597, 79);
        Rock rock4 = new Rock();
        addObject(rock4, 851, 424);
        Rock2 rock22 = new Rock2();
        addObject(rock22, 897, 141);
        Rock2 rock23 = new Rock2();
        addObject(rock23, 592, 263);
        Rock2 rock24 = new Rock2();
        addObject(rock24, 364, 144);
        Rock2 rock25 = new Rock2();
        addObject(rock25, 111, 330);
        people people = new people();
        addObject(people, 810, 53);
        people people2 = new people();
        addObject(people2, 930, 302);
        people people3 = new people();
        addObject(people3, 502, 435);
        people people4 = new people();
        addObject(people4, 417, 216);
        people people5 = new people();
        addObject(people5, 191, 443);
        people people6 = new people();
        addObject(people6, 213, 78);
        people people7 = new people();
        addObject(people7, 723, 279);
        people people8 = new people();
        addObject(people8, 499, 100);
        people people9 = new people();
        addObject(people9, 713, 442);
        people people10 = new people();
        addObject(people10, 68, 445);
        people people11 = new people();
        addObject(people11, 48, 96);
        people people12 = new people();
        addObject(people12, 245, 254);
        removeObject(rock25);
        removeObject(rock24);
        Rock2 rock26 = new Rock2();
        addObject(rock26, 759, 74);
        Counter counter = new Counter();
        addObject(counter, 90, 475);
        counter.setLocation(82, 469);
    }
}
Okay there Is the code where I get the message
danpost danpost

2013/10/17

#
Move the last 3 lines (creating and adding the Counter object into the world) to the beginning of the method. The counter field must be declared before you can supply it to the Rocket object.
quicklearner quicklearner

2013/10/17

#
Okay now when I compile it it says, "constructor Rocket in class Rocket cannot be applied to given types: required:no arguments; found:Counter; reason:actual and formal argument lists differ in length" This time it highlights "new Rocket(counter)"
Gevater_Tod4711 Gevater_Tod4711

2013/10/17

#
You need to add a new constructor in your Rocket class. Add this to your Rocket and it should work:
1
2
3
4
5
6
7
8
public class Rocket extends Actor {
     
    private Counter counter;
     
    public Rocket(Counter counter) {
        this.counter = counter;
    }
}
quicklearner quicklearner

2013/10/17

#
Okay Thanks A lot to You Both! Got It working = Happy me!!!!!!!!
You need to login to post a reply.