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

2012/11/20

Add a counter to a code

avocasso avocasso

2012/11/20

#
I am a new learner in Greenfoot. Can anyone help me with how I can add a counter so that my object changes images every n times the Act method is invoked?
Zamoht Zamoht

2012/11/20

#
public int counter = 0; And then in the act method add
public void act()
{
     if (counter == n)
     {
          setImage(the image you want to change to);
          counter = 0;
     }
     else
     {
          counter++;
     }
}
This might not be what you are looking for, but maybe it helps and gives you an idea of how it could be done. One thing this would only change the image once since I only added one setImage( image );
SPower SPower

2012/11/20

#
@Zamoth instead of this:
public int counter = 0;
this would be better:
private int counter = 0;
;)
Zamoht Zamoht

2012/11/20

#
Yes you are right I just forget thinking about wether I should make a reference private or public since I don't know what benefits you get from making it private. I know that other objects can't change the value of a private, but what benefits you get from that I don't know. Btw thanks for correcting me (:
Zamoht Zamoht

2012/11/20

#
(privates are hidden to other objects)
SPower SPower

2012/11/20

#
I know, but it's a better code design to make them private always, and let others access them using setter and getter methods. It's just like real life: if you need to make a painting, you don't want others to be able to mess around with it! :)
avocasso avocasso

2012/11/20

#
So SPower, I am not sure if I understand your approach. Are you suggesting that I have this: 01. public void act() 02. { 03. if (counter == n) 04. { 05. setImage(the image you want to change to); 06. private int counter = 0; 07. } 08. else 09. { 10. counter++; 11. } 12.}
avocasso avocasso

2012/11/20

#
or I should have changed public void act() to private void act()
SPower SPower

2012/11/20

#
No, I meant this:
private int counter = 0;

public void act()
{
     if (counter == n)
     {
          setImage(the image you want to change to);
          counter = 0;
     }
     else
     {
          counter++;
     }
}
which is just a bit improved from zamoht.
avocasso avocasso

2012/11/20

#
ok let me try that
avocasso avocasso

2012/11/20

#
So I tried it, it works. Thanks ! But in the meantime my object changed images only one time every four times that the Act method is invoked, even though I specified counter == 4, in other word I wanted the object to change every 4 times the Act method is invoked. Is there a way to make the object doing the change continuously, ie after every 4 times go back to the initial image where we started from and do it over again?
avocasso avocasso

2012/11/20

#
I am sorry if my question seems silly.
danpost danpost

2012/11/20

#
To cycle through the images one per act method
// with instance field
private int counter=0;
// and in act
counter=(counter+1)%4;
if(counter==0)setImage("image1.png");
if(counter==1)setImage("image2.png");
if(counter==2)setImage("image3.png");
if(counter==3)setImage("image4.png");
Line 4 above is equivalent to
counter=(counter+1)-4*((counter+1)/4)
essentially getting the remainder of the incremented value divided by four.
avocasso avocasso

2012/11/20

#
Thank you All. That helps !
You need to login to post a reply.