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

2015/1/15

Slight Problem Getting Counter To Work

Manda_x Manda_x

2015/1/15

#
public class Counter extends Actor
{
    int points=0;
   
    public void act()
    {
        text();
        
    }
    public void text()
    {
        setImage(new GreenfootImage("Score:"+ points,35,Color.GREEN,Color.BLACK));
    }
    public void addPoints()
    {
        points++;
    }

    }
Super_Hippo Super_Hippo

2015/1/15

#
Either just put line 16 in the act method or call the 'addPoints' method from there if it just has to count up. If there are special events which cause the increasing of the score, then you should call the method right then.
Manda_x Manda_x

2015/1/15

#
I added 'addPoints' method inside my act method now the counter increases without the interaction of my targets. There are falling targets that I want to increase the score but its currently adding points by its self.
danpost danpost

2015/1/15

#
I have been seeing a lot of this kind of counter lately where the act method is used to continuously reset its image. The image only needs changed when the value of the counter changes. Also, the name of the method, 'text', is not a very clear name to indicate what the method does; a better name might be 'updateImage'. Remove the 'act' method and call the method that updates the image after incrementing the 'points' field in the 'addPoints' method. That is (using 'updateImage' as the name of the 'text' method):
public void addpoints()
{
    points++;
    updateImage();
}
Now, to avoid having a greenfoot icon show up for your counter before the 'Run' button is pressed, add a constructor to the class of the counter to initially set the image of the objects of that class:
public Counter()
{
    updateImage();
}
Manda_x Manda_x

2015/1/15

#
thanks the counter works now and adding the constructer to the class of the counter has set the image before pressing the run button. But the counter is now increasing points on its own without adding a point for every target collected. it's not working with my game but on its own.
Super_Hippo Super_Hippo

2015/1/15

#
Only call the method on this Counter object when the interaction occurred. Do not call it from the act method if you don't want it to increase score by itself.
Manda_x Manda_x

2015/1/15

#
show me, cause I don't understand
danpost danpost

2015/1/16

#
danpost wrote...
Remove the 'act' method
Manda_x Manda_x

2015/1/16

#
public class Counter extends Actor
{
    int points=0;
   
    public void act()
    {
        updateImage();
        addPoints();
    }
    public void updateImage()
    {
        setImage(new GreenfootImage("Score:"+ points,35,Color.GREEN,Color.BLACK));
    }
    public void addPoints()
    {
        points++;
        updateImage();
    }
    public Counter()
    {
        updateImage();
    }
    

    }

I removed the entire act method but then nothing happened when I pressed run game.
danpost danpost

2015/1/16

#
Your act method is still in the class! Remove lines 5 through 9. Then, it will be up to the actor that collects the targets to increment the counter.
Manda_x Manda_x

2015/1/16

#
public class Counter extends Actor
{
    int points=0;
    public void updateImage()
    {
        setImage(new GreenfootImage("Score:"+ points,35,Color.GREEN,Color.BLACK));
    }
    public void addPoints()
    {
        points++;
        updateImage();
    }
    public Counter()
    {
        updateImage();
    }
}
my actor is collect the targets but the number in score isn't changing, sorry I posted the wrong code before.
Super_Hippo Super_Hippo

2015/1/16

#
The following line will call the method on this objects. It has to be executed whenever you collect a target. So after your actor removes the target, place this line:
((Counter)getWorld().getObjects(Counter.class).get(0)).addPoints();
(Actually you only add one point, so maybe the method's name should be 'addPoint'.)
Manda_x Manda_x

2015/1/16

#
okay i'll try it now
Manda_x Manda_x

2015/1/16

#
thank you guys its now working
You need to login to post a reply.