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

2013/3/27

This simple thing just won't work..

1
2
Wescorn Wescorn

2013/3/27

#
So i am making a farmer game, and i have this problem.. I took the trick-the-turtle scenario, and renamed the "tryEat" method to "collect". I have 3 actors.. a Farmer, a House and Grain. I wan't the player to control the farmer and go out and harvest some grain, and when the farmer has collected 20 grain, he is full and has to go store the grain in the house. It all works, except he won't drop off the grain when he is at the house. The problem is probably at the buttom of the code.
import greenfoot.*; 

public class Farmer extends Animal
{

private int grainCollected = 0;
private int grainStored = 0;

    public void act()
    {
        
        if (Greenfoot.isKeyDown("D") ) {
            if (Greenfoot.isKeyDown("S") ) {
                setRotation(45);
                move(1); }
            else if (Greenfoot.isKeyDown("W") ) {
                setRotation(315);
                move(1); }
            else {
                setRotation(0);
                move(1); }
        }
        else if (Greenfoot.isKeyDown("A")) {
            if (Greenfoot.isKeyDown("S") ) {
                setRotation(135);
                move(1); }
            else if (Greenfoot.isKeyDown("W") ) {
                setRotation(225);
                move(1); }
            else {
                setRotation(180);
                move(1); }
        }

        else if (Greenfoot.isKeyDown("S")) { 

            setRotation(90);
            move(1);
        }
        else if (Greenfoot.isKeyDown("W")) { 

            setRotation(270);
            move(1);
        }
        if (canSee(Grain.class)) {
            if (grainCollected < 20) {
            collect(Grain.class);
            grainCollected ++;
        }  
        if (canSee(House.class)) {
               grainStored = grainCollected + grainStored;
               grainCollected = 0;
    }
}

}
}
Help please (:
JetLennit JetLennit

2013/3/27

#
you could try finding the coordinates of the house and use the getX() and the getY() methods with somthing like
if(getX = the houses x && getY = the houses y)
{
  grainStored = grainCollected + grainStored;
               grainCollected = 0;
}
Wescorn Wescorn

2013/3/27

#
ill try, just a minute
Wescorn Wescorn

2013/3/27

#
I tried this, but it says "Cannot find symbol - variable getX"
        if(getX = 104 && getY = 86) { 
               grainStored = grainCollected + grainStored;
               grainCollected = 0;
maybe i did it wrong?
JetLennit JetLennit

2013/3/27

#
you I forgot the () after each
JetLennit JetLennit

2013/3/27

#
sorry about that... i do that all the time!
-nic- -nic-

2013/3/27

#
    if(getX() == 104 && getY() == 86) {   
           grainStored = grainCollected + grainStored;  
           grainCollected = 0;  
Change it to this
Wescorn Wescorn

2013/3/27

#
What, you mean like
        if(getX() = 104 && getY() = 86) { 
               grainStored = grainCollected + grainStored;
               grainCollected = 0;
Now it says unexpected type required: variable; found: value
-nic- -nic-

2013/3/27

#
also change the = in the if statement to ==
Wescorn Wescorn

2013/3/27

#
Now it can compile but it didn't fix the problem :c
JetLennit JetLennit

2013/3/27

#
sorry about that...
JetLennit JetLennit

2013/3/27

#
is it that it just wont add to the grain in the farm or will it not detect the farm?
Wescorn Wescorn

2013/3/27

#
Yeah, when the farmer goes to the house, nothing happens
JetLennit JetLennit

2013/3/27

#
okay... i thought it was a detection problem... are you intending to post it? (it is actually important)
-nic- -nic-

2013/3/27

#
what you are trying to test might be too hard to achive you could:
if(!getOneIntersectingObject(House.class)==null)
{
  //code for grain collect
}
There are more replies on the next page.
1
2