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

2015/8/18

help with counter class

Jeph Jeph

2015/8/18

#
I imported the counter class and i been fooling around with it but getting no where ,its a simple shoot the balloon game with about 3 levels,which is a subclass of the main world (helperclass)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class helperClass extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class helperClass.
     *
     */
    public helperClass()
    {   
      
        super(600, 400, 1,false);
        GreenfootImage bg = getBackground();
        bg.setColor(Color.GREEN);
        bg.fill();
 
        prepare();
        
    }
public Counter getCount()
{
    return counter;
}
}
1
2
3
4
5
6
7
8
9
10
11
  public void eat()
    {
//in my actor class
        eat(balloon.class);
         
        helperClass Hclass = (helperClass)getWorld();
        Counter counter = Hclass.getCount();
        counter.getCount();
        counter.getValue();
 
    }
if theres another easy way to get the counter running do tell :) and thanks in advance
danpost danpost

2015/8/18

#
Lines 8 and 9 in the 'eat' method both have the same problem. They each appear to 'get' a value without applying that value anywhere (not saving it in a local variable for use later and not using it immediately). They are essentially no-op commands in that thy perform no meaningful operations by themselves. You should look over the Counter class to see what methods are available and what each does. Open the editor on the class and in the upper-right, change the drop-down value from 'Source' to 'Documentation' to view the class in a more useful way.
Jeph Jeph

2015/8/19

#
i have looked at the documentation as it did help for a better understanding but still noting :?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int count= 0;
 
 public void count()
    {
        count++;
    }
 
 
 
public void eat()
    {
        Counter counter = new Counter();
        if(canSee(balloon.class))
        {
            
           eat(balloon.class);
            count();
            counter.add(count);
            counter.setValue(count);
        }
}
danpost danpost

2015/8/19

#
Your original code-post look closer to what you want than your last one. In the last one: * the count is maintained within the Counter class by the Counter object you create * creating a counter object each time an object is eaten will have a different 'count' field to be update each time by lines 17 through 20 * any Counter object created by line 12 will not be in your world All you really needed to do was to replace lines 8 and 9 in the 'eat' method of your first post to:
1
counter.add(1);
Jeph Jeph

2015/8/19

#
ohk so i tried to put things back to my 1st post with what you said and am still not getting this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//am i calling this right?
public class helperClass extends World
{
    Counter counter = new Counter();
public Counter getCount()
{
    return counter;
}
}
 
  
public void eat()
    {
     eat(balloon.class);
     helperClass Hclass = (helperClass)getWorld();
     Counter counter = Hclass.getCount();
     counter.add(1);
     }
danpost danpost

2015/8/19

#
Jeph wrote...
ohk so i tried to put things back to my 1st post with what you said and am still not getting this
What you are now showing looks good. If it is still not working, you will need to show more of your code. Maybe you are not adding 'counter' into the world and are adding 'new Counter()' instead (which you should not)?
Jeph Jeph

2015/8/20

#
this is all the code that relates to the counter class
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
//in my main world
public class helperClass extends World
{
    Counter counter = new Counter();
public Counter getCount()
{
    return counter;
}
 
 
private void prepare()
    {
 
        Counter counter = new Counter();
        addObject(counter, 57, 32);
   }
}
 
//in my actor
public class shoot extends Animal
{
  public void act()
    {
      eat();
     }
public void eat()
    {
          eat(balloon.class);
           helperClass Hclass =          (helperClass)getWorld();
        Counter counter = Hclass.getCount();
       counter.add(1);
    }
}
danpost danpost

2015/8/20

#
Jeph wrote...
this is all the code that relates to the counter class
Remove line 14. By creating a local variable within the method, 'counter' will not refer to the field declared on line 4.
Jeph Jeph

2015/8/21

#
its works! thank you!! and for your time :P
You need to login to post a reply.