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

2016/12/19

Help with including code in question

calemip calemip

2016/12/19

#
Would someone assist me please. I want to include (upload) code along with my coding question, however, I can't find any reference on how to do this. Thanks
danpost danpost

2016/12/19

#
calemip wrote...
I want to include (upload) code along with my coding question, however, I can't find any reference on how to do this.
There is a link below the 'Post a reply' box below for this.
Nosson1459 Nosson1459

2016/12/20

#
Need help with embedding? click here! Posting code? read this! (It says the same exact thing right under the "Post a reply" box at the bottom of this page (only when a/the user is logged in).)
calemip calemip

2016/12/21

#
Thanks very much for the direction.
Nosson1459 Nosson1459

2016/12/21

#
calemip wrote...
I want to include (upload) code along with my coding question
What about the question?
calemip calemip

2016/12/23

#
Hello. Here is some code I am having problems with. I have read and read and tried out different methods before asking this question but I just can't find what I am doing wrong. I know it must be something basic though., but the answer eludes me. Currently, if Pengu eats Broccoli or Nuts he gets 5 points for each one eaten - works great. I want to implement two separate counters, one for Broccoli and one for Nuts. Below is code for scenario which works fine and also my attempt to get scenario working with 2 counters. Thank you.
import greenfoot.*;

/**
 * Working Scenario. Pengu has nuts and broccoli to eat. Can move left or right.
 */
public class Pengu extends Actor
{
    private int points =0;
    private Counter counter;     

    /**
     * Counter to count number of nuts and broccoli eaten. 5 points for each eaten.
     * 
     */public Pengu (Counter pointCounter)
    {
        counter = pointCounter;
    }

    public void act() 
    {
        checkKeys();   
        tryToEatNut();    
        tryToEatBroccoli();         
    }

    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("pengu-left.png");
            move(-4);
        }
        if (Greenfoot.isKeyDown("right") )
        {
            setImage("pengu-right.png");
            move(4);
        }

    }    

    public void tryToEatNut()
    {
        if (canSee(Nut.class))
        {
            eat(Nut.class);
            points++;
            counter.add(5);            
        }
    }

    public void tryToEatBroccoli()
    {
        if (canSee(Broccoli.class))
        {
            eat(Broccoli.class);
            points++;
            counter.add(5);         
        }
    }

    /**
     * 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;        
    }

    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
}
Below is code that does not work. Can't get second counter working.
import greenfoot.*;

/**
 * Non-working Scenario. Pengu has nuts and broccoli to eat. Can move left or right.
 */
public class Pengu extends Actor
{
    private int points =0;
    private Counter counter;     
    private Counter counter1; 

    /**
     * Counter to count number of nuts and broccoli eaten. 5 points for each eaten.
     * 
     */public Pengu (Counter pointCounter)
    {
        counter = pointCounter;
    }

    
    /**
     * Counter to count number of nuts and broccoli eaten. 5 points for each eaten.
     * 
     */public Pengu (Counter pointCounter1)
    {
        counter1 = pointCounter1;
    }
    public void act() 
    {
        checkKeys();   
        tryToEatNut();    
        tryToEatBroccoli();         
    }

    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("pengu-left.png");
            move(-4);
        }
        if (Greenfoot.isKeyDown("right") )
        {
            setImage("pengu-right.png");
            move(4);
        }

    }    

    public void tryToEatNut()
    {
        if (canSee(Nut.class))
        {
            eat(Nut.class);
            points++;
            counter.add(5);            
        }
    }

    public void tryToEatBroccoli()
    {
        if (canSee(Broccoli.class))
        {
            eat(Broccoli.class);
            points++;
            counter1.add(5);         
        }
    }

    /**
     * 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;        
    }

    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
}
Nosson1459 Nosson1459

2016/12/23

#
You have two pengu constructors both with the same name they are both
public pengu(Counter)
with a variable in it, the name and the parameters are the same even though the name of the Counter variable is different. To do what you are attempting (I don't know about the rest of the code) try this:
/**
 * Counter to count number of nuts and broccoli eaten. 5 points for each eaten.
 * 
 */
public Pengu (Counter pointCounter, Counter pointCounter1)
{
    counter = pointCounter;
    counter1 = pointCounter1;
}
You should have a syntax error on the second pengu constructor that you didn't mention, it all has to be in the same constructor.
calemip calemip

2016/12/24

#
Thanks. Got it working. and it makes sense now. I was getting too complicated as well.
You need to login to post a reply.