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

2014/1/6

Adding 2 counters in a game.

Onyousang Onyousang

2014/1/6

#
In my game, I've added a counter in no problem to count how much ammo I have left, however, when I try to add a new counter for a separate actor, it says "cannot find symbol- variable redCounter" and I have an actor with the name RedCounter and it has the same code as the normal counter but has been tweaked to compile. Here's the code in the actor which has the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void shoot()
   {
       if (Greenfoot.isKeyDown("shift"))
       {
           if (redCounter.getValue() >0)
           {
               RedShot redShot = new RedShot();
               getWorld().addObject(redShot,getX(),getY());
               redShot.setRotation(getRotation());
               redCounter.add(-1);
           }
       }
   }
and in comparison I have another code showing an alternate actor with virtually the same code in my game:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void shoot()
   {
       if (Greenfoot.isKeyDown("space"))
       {
           if (counter.getValue() >0)
           {
               Shot shot = new Shot();
               getWorld().addObject(shot,getX(),getY());
               shot.setRotation(getRotation());
               counter.add(-1);
           }
       }
   }
Someone help me on why I'm getting this error.
bourne bourne

2014/1/6

#
Show us where redCounter is defined for the top one.
Onyousang Onyousang

2014/1/6

#
It's defined in the actor class.
bourne bourne

2014/1/6

#
That doesn't help. I don't see anything wrong with the code given. And what line throws the Exception "cannot find symbol- variable redCounter"?
Onyousang Onyousang

2014/1/6

#
Anything with the "redcounter" inside it throws a variable tantrum. The code works perfectly in an another actor with a near identical code. Here is the code of the actor "RedTank" and its counter.
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
/**
 * Write a description of class RedTank here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class RedTank extends Actor
{
    /**
     * Act - do whatever the RedTank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        isKeyDown();
        shoot();
        checkForReload();
    }
    public void isKeyDown()
    {
       if ( Greenfoot.isKeyDown("a") )
        {
            turn(-2);
        }
       if ( Greenfoot.isKeyDown("d") )
        {
            turn(2);
        }
        if ( Greenfoot.isKeyDown("w") )
        {
            move(2);
        }
        if ( Greenfoot.isKeyDown("s") )
        {
            move(-2);
        }
    }
    public void shoot()
    {
        if (Greenfoot.isKeyDown("shift"))
        {
            if (redCounter.getValue() >0)
            {
                RedShot redShot = new RedShot();
                getWorld().addObject(redShot,getX(),getY());
                redShot.setRotation(getRotation());
                redCounter.add(-1);
            }
       }
    }
 
    public RedTank (RedCounter pointsCounter)
    {
        redCounter = pointsCounter;
        redCounter.add(100);
    }
 
    /**
     * Return true if we can see an object of class 'clss' right where we are.
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;       
    }
 
    public void checkForReload()
    {
        if (waitCount >=200)
        {
            if (canSee(Ammo.class))
            {
                waitCount = 0;
                counter.add(100);
            }
        }
 
    }
}
And here is it's counter:
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
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class RedCounter extends Actor
{
     private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
 
    /**
     * Create a new Red counter, initialised to 0.
     */
    public RedCounter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
     
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act()
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }
 
    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }
 
    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
 
    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
 
    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("Ammo" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
And here is the GreenTank with the counter:
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
/**
 * Write a description of class GreenTank here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GreenTank extends Actor
{
    private int waitCount;
    private Counter counter;
    /**
     * Act - do whatever the GreenTank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        isKeyDown();
        shoot();
        waitCount++;
        checkForReload();
    }
 
    public void isKeyDown()
    {
        if ( Greenfoot.isKeyDown("left") )
        {
            turn(-2);
        }
        if ( Greenfoot.isKeyDown("right") )
        {
            turn(2);
        }
        if ( Greenfoot.isKeyDown("up") )
        {
            move(2);
        }
        if ( Greenfoot.isKeyDown("down") )
        {
            move(-2);
        }
    }
 
    public void shoot()
    {
        if (Greenfoot.isKeyDown("space"))
        {
            if (counter.getValue() >0)
            {
                Shot shot = new Shot();
                getWorld().addObject(shot,getX(),getY());
                shot.setRotation(getRotation());
                counter.add(-1);
            }
        }
    }
 
    public GreenTank (Counter pointsCounter)
    {
        counter = pointsCounter;
        counter.add(100);
    }
 
    /**
     * Return true if we can see an object of class 'clss' right where we are.
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;       
    }
 
    public void checkForReload()
    {
        if (waitCount >=200)
        {
            if (canSee(Ammo.class))
            {
                waitCount = 0;
                counter.add(100);
            }
        }
 
    }
}
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
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
     private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
 
    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
     
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act()
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }
 
    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }
 
    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
 
    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
 
    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("Ammo" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
bourne bourne

2014/1/6

#
GreenTank has: private Counter counter; yet RedTank doesn't have: private RedCounter redCounter;
Onyousang Onyousang

2014/1/6

#
Thank! It worked!
You need to login to post a reply.