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

2011/10/3

what is a counter on greenfoot?

1
2
faizakamal faizakamal

2011/10/3

#
If you have 'Introduction to Programming with Greenfoot' book by Michael Kolling, could you please help with Exercise 4.18 on page 56? It regards the crab scenario. It says: 'The crab image changes fairly quickly while the crab runs, which makes our crab look very hyperactive. Maybe it would look nicer if the crab image changed only on every second or third act cycle. Try to implement this. To do this, you could add a counter that gets incremented in the act method. Every time it reaches 2 (or 3), the image changes, and the counter is reset to 0.' Please help. Thank You
actinium actinium

2011/10/4

#
Maybe an easier way for you to achieve this is to use Greenfoot.getRandomNumber(n) and do the animation only when the random number equals a selected number. The counter way is to create an instance variable, set the instance variable to zero in the constructor, increment the instance variable in the act method or in a user method called from the act method, check the instance variable, do animation if this variable is equal to your desired comparison, reset the instance variable back to zero when the animation sequence is finished. I think that should do it.
davmac davmac

2011/10/4

#
A counter is just a variable (usually of type "int") which is used for counting. You declare it just like you declare any other variable, and you increment it by adding one to its value:
// declare the variable:
private int counter = 0;

...


// increment the counter:
counter = counter + 1;

// alternative way to increment counter:
counter++;
rgadue rgadue

2011/10/5

#
How about using it for this scenario: "Write a loop that subtracts one from the Dog’s energy for every 30 steps that the Dog takes. " I have: public class MyDog extends Dog { private int energy = 100; private boolean amSleepy = false; private int count_steps = 0; for (int i = 0; i < 20; i++){ count_steps++; if (count_steps =30) energy--; } But no go, receiving: illegal start type and the for line is highlighted.
davmac davmac

2011/10/5

#
rgadue - most statements, including for loops, can only appear inside methods. You have a class, but you haven't defined a method within it. You are also using an assignment (=) in the 'if' clause instead of an equality comparison (==). Try this:
public class MyDog extends Dog
{
    private int energy = 100;
    private boolean amSleepy = false;
    
    public void act()
    {
        private int count_steps = 0;
        for (int i = 0; i < 20; i++) {
            count_steps++;
            if (count_steps == 30) {
                energy--;
            }
        }
    }
}
(It might not do exactly what you want - but it should compile at least).
rgadue rgadue

2011/10/6

#
davmac - thanks for the help. I got it to compile and now I'm struggling with my next step. 8. Write a loop that subtracts one from the Dog’s energy for every 30 steps that the Dog takes. 9. Make the player-controlled Dog move more slowly whenever the method isTired returns a value of true. 10. Force the player-controlled Dog to fall asleep when its energy reaches 0, and once it wakes up, set its energy to 100. This is what I have so far: public class MyDog extends Dog { private int energy = 100; private boolean amSleepy = false; private int distance; /** * Act - do whatever the MyDog wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); lookForDog(); isSleepy(); int count_steps = 0; for (int i = 0; i < distance; i++) { count_steps++; if (count_steps == 30) { energy--; } } } /** * This dog gets sleepy when its energy hits 20 or lower. */ public boolean isSleepy() { if ( energy <= 20 ) { amSleepy = true; } else { amSleepy = false; } return amSleepy; } /** * Check whether a key has been pressed and if so move the dog accordingly. Animate dog by switching between images. * Dog moves north, south, east, west, northeast, northwest, southeast, and southwest. */ public void checkKeypress() { if (Greenfoot.isKeyDown("up")) { setRotation(90); if(isSleepy()==true){ move(-1); stepsTaken = stepsTaken +1; } else { move(-5); } setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } else { setImage("dog.png"); } if (Greenfoot.isKeyDown("down")) { setRotation(-90); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("left")) { setRotation(0); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("right")) { setRotation(0); move(5); setImage("dog-walk-right.png"); wait(2); setImage("dog-walk-right-2.png"); } } /** * Return true if we can see an object of class 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; } /** * Look for stationary dogs and greet (bark) when interacting. */ public void lookForDog() { if (canSee(Dog.class)) { Greenfoot.playSound("chasdog.wav"); setImage("dog-bark-1.png"); wait(6); setImage("dog-bark-2.png"); wait(6); setImage("dog.png"); } } }
Wolfman71 Wolfman71

2011/11/4

#
Hello, I am trying to do the same scenario and I am stuck at the same point that rgadue was at. I am trying to make the dog sleep when his energy reaches zero but I can't get his energy to deplete. When he is sleeping I need his energy to replenish. I have a counter but it doesn't seem to count. I also need to make the dog interact with other dogs in the world but I can't get him to. It all compiles but it just won't do what I want. These are my instructions: 1. Write a loop that subtracts one from the Dog’s energy for every 30 steps that the Dog takes. 2. Make the player-controlled Dog move more slowly whenever the method isTired returns a value of true. 3. Force the player-controlled Dog to fall asleep when its energy reaches 0, and once it wakes up, set its energy to 100. Any help will be greatly appreciated. This is how mine looks: public class MyDog extends Dog { private String image = "dog.png"; private int energy = 100; private boolean isTired = false; private int countSteps; /** * Act - do whatever the MyDog wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); isTired(); int countSteps = 0; for (int counter = 0; counter < 1; counter++) { countSteps = countSteps + 1; if (countSteps == 100) { energy--; } } } /** * This dog gets sleepy when its energy hits 20 and needs to walk slower. */ public boolean isTired() { if ( energy == 0 ) { isTired = true; move(-5); } else { isTired = false; } return isTired; } /** * I sleep for a specified length of time and snore too! */ public void sleep(int duration) { Greenfoot.playSound("Snore1.wav"); for (int dur = 0; dur <= 10; dur++) { for (int i = 1; i <=4; i++) { setImage("dog-sleep-" + i + ".png"); wait(4); } } setImage(image); isTired = true; } /** * Check whether a key has been pressed and if so move the dog accordingly. * Animate dog by switching between images. * Dog moves north, south, east, west, northeast, northwest, southeast, and southwest. */ public void checkKeypress() { if (Greenfoot.isKeyDown("up")) { setRotation(90); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("down")) { setRotation(-90); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("left")) { setRotation(0); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("right")) { setRotation(0); move(5); setImage("dog-walk-right.png"); wait(2); setImage("dog-walk-right-2.png"); } } /** * Return true if we can see another dog. * Return false if there is no such object here. */ public boolean canSee(Class clss) { Actor Mydog = getOneObjectAtOffset(0, 0, clss); return Mydog != null; } /** * Look for other dogs and greet them. */ public void lookForDog() { if (canSee(Dog.class) ) { Greenfoot.playSound("chasdog.wav"); setImage("dog-bark-1.png"); setImage("dog-bark-2.png"); setImage("dog.png"); } } }
danpost danpost

2011/11/4

#
@Wolfman71 -- Take the 'int countSteps = 0;' out of the act() method and when declaring 'private int countSteps;' make it 'private int countSteps = 0;'.
Wolfman71 Wolfman71

2011/11/6

#
Thank you for the reply danpost but I still can't make a loop that takes 1 away from the dog's energy when he takes 30 steps. Can somebody please help me with one or set me in the right direction?
kiarocks kiarocks

2011/11/6

#
public class MyDog extends Dog
{
    private String image = "dog.png";
    private int energy = 100;
    private boolean isTired = false;
    private int countSteps;
     
    /**
     * Act - do whatever the MyDog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeypress();
       
       isTired();
        
            countSteps = countSteps + 1;
            if (countSteps == 30)
            { 
            energy--;
            }
        
    }
}
Tell me if this does what you want
Wolfman71 Wolfman71

2011/11/6

#
It does work in a way. I open the inspect window of the dog and then run the game. The steps count up without the dog moving and the energy only drops 1 point and stays at 99. I need to have the energy continuously drop a point every 30 steps until it reaches zero. This is basically what I have been struggling with.
Wolfman71 Wolfman71

2011/11/6

#
Alright here we go. After seeking out some help I have found a solution of sorts. Here is what was missing: countSteps = countSteps + 1; if (countSteps == 30) { energy--; countSteps = 0; } the "countSteps = 0;" to reset the steps to zero and start counting again to create the loop. Thank you to all that helped and I hope this little tidbit helps others who experience the same type of problem. :)
Lildarkone Lildarkone

2011/11/7

#
remember if you are implementing a larger value that isnt divisible by 30, say countsteps = countSteps +7, you have to to change the if statement to include values over and around 30. if(countSteps >= 30) will make sure that the loop initiates. also, if you are implementing by 1, just use countSteps++; If you want to keep it the way you have, countSteps += 1 is more efficient and does the same thing as contSteps = contSteps +1;
Wolfman71 Wolfman71

2011/11/8

#
Thank you for the tips. The info can be useful once I get more comfortable and I delve more deeply into making more complicated stuff. I am a beginner right now but hopefully I will make some worthwhile stuff in the future.
CHERNANDEZ95 CHERNANDEZ95

2013/1/29

#
Hi everyone, I am new to Greenfoot and was hoping I could also get some assistance with the mentioned book problem. I need to create a counter within the act method that changes the image every time it reaches 3, then resets itself to 0. This is what I have so far. public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; private int counter; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); move(); lookForWorm(); if(counter == 3) { counter = 0; switchImage(); } } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } I would greatly appreciate any help.
There are more replies on the next page.
1
2