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

2012/2/20

how to manually "Feed" an object

CCLong2010 CCLong2010

2012/2/20

#
OK so I am trying to manually feed (add energy) my "friendly cat" to have him wake up. After 10 steps with out eating the pizza he falls asleep and you have to manually feed him to wake him up. Here is the code I have so far...any help would be appreciated. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * * FriendlyCat describes cats that * - move and jump when corresponding control keys are pressed, when having enough energy * - love pizzas and show their joy by dancing after eating three pizzas * */ public class FriendlyCat extends Cat { private int pizzaEaten = 0; private int energy = 10; public FriendlyCat() { energy = 10; pizzaEaten = 0; } public void act() { { if (energy <= 0) { sleep(1); return; } } findPizza(); if (Greenfoot.isKeyDown("left")) { walkLeft(1); energy --; } if (Greenfoot.isKeyDown("right")) { walkRight(1); energy --; } if (Greenfoot.isKeyDown("down")) { jumpDown(1); energy --; } if (Greenfoot.isKeyDown("up")) { jumpUp(1); energy --; } if (energy <= 0) { sleep (1); } } public void findPizza() { if (canSee(Pizza.class)) { eat(); remove(Pizza.class); pizzaEaten ++; howManyEaten(); energy = energy + 5; } } public void howManyEaten() { if (pizzaEaten == 3) { dance(); pizzaEaten = 0; } } }
danpost danpost

2012/2/20

#
Before addressing your question, let me point out that the last few statements in your act() method
if (energy <= 0)
{
    sleep(1);
}
are not needed, as that code is at the beginning of the act() method. Now, to your question, which was rather vague. How are you to manually feed the friendly cat? Are you supposed to drag a pizza to its nose? or maybe click a button to add the energy? or stop the scenario and select a method that will add energy to the cat and then re-start the scenario (this one I doubt, but you really did not say)? Anyway, how (by what mechanism)?
Duta Duta

2012/2/20

#
@danpost, I assume from this that the cat will eat a pizza when it is intersecting a pizza:
public void findPizza()
{
    if (canSee(Pizza.class))
    {
        eat();
        remove(Pizza.class);
        pizzaEaten ++;
        howManyEaten();
        energy = energy + 5;
    }
}
because from the look of the code it seems to be built on top of the Wombats tutorial scenario, and there's (unless I'm not mistaken) a method along the lines of:
public void canSee(java.lang.Class clss)
{
    return getOneIntersectingObject(clss) != null;
}
My code may not be exactly the same, but that was the gist of it if I remember rightly. @CCLong2010 First off I recommend changing the method to the following:
public void findPizza()
{
    Pizza pizza = getOneIntersectingObject(Pizza.class);
    if(pizza != null)
        eatPizza(pizza);
}

public void eatPizza(Pizza pizza)
{
    eat();
    if(pizza != null) getWorld().removeObject(pizza);
    pizzaEaten++;
    howManyEaten();
    energy += 5;
}
Then to manually feed a cat a pizza (which I don't really know what you mean by that, but I'm taking it to mean feed a cat a pizza when it isn't intersecting one) you just call
eatPizza(null);
//Or if you want to feed a pizza somewhere else in the world
Pizza pizza = //A reference on the pizza you want to feed to the cat ;
eatPizza(pizza);
This gives the effects of eating a pizza. One thing is I changed remove(Pizza.class) to getWorld().removeObject(pizza) because, unless there's a method remove(java.lang.Class clss) in the superclass Cat (or a superclass of that) then its not going to work. Even if there is one, its giving a reference on the Pizza class, rather than an individual pizza object. Also what do eat() and sleep(int i) do? (I assume they is a superclass methods too...) Oh and one final thing:
private int pizzaEaten = 0;
private int energy = 10;
public FriendlyCat()
{
    energy = 10;
    pizzaEaten = 0;
}
could just be:
private int pizzaEaten = 0, energy = 10;
The comma indicates that energy is also a private int, and I've removed the constructor because it was useless - the variables are already set directly after they are declared, so you don't have to re-set them in the constructor. EDIT: By the way, use code tags around your code to make it more readable - not necessary, but its nice from our point of view when we're reading it.
CCLong2010 CCLong2010

2012/2/21

#
Sorry for the vagueness I am new at this.....what I mean by manually feed the cat is that I have to drag a piece of pizza to his mouth, he has to eat it and it has to wake him up and add enough energy for 5 steps. I see the duplication in my program that you all are referencing I will fix that now....Thx
CCLong2010 CCLong2010

2012/2/21

#
Thanks Duta you are right I did use wombat world as a guide through out some of this code. Thanks for all of the information and I will go back and add the code tags.
danpost danpost

2012/2/21

#
The code tags that Duta was referring to, cannot be applied to the previous posts. But, in the future, when posting code as you did above, put immediately after the code and
danpost danpost

2012/2/21

#
Had I put them in standard order, it would have come out like: ...when posting code as you did above, put
 immediately before the code and 
immediately after the code.
JJEagle JJEagle

2012/2/21

#
@Duta Where does this section go under? Like which Public void (...) does it go under? eatPizza(null); //Or if you want to feed a pizza somewhere else in the world Pizza pizza = //A reference on the pizza you want to feed to the cat ; eatPizza(pizza); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Rashmi Rashmi

2012/2/21

#
I tried to download and get going with greenfoot , and the message displayed is "This is read-only file." I have downloaded JDK prior to greenfoot. pl. Suggest me to get working with greenfoot.
Duta Duta

2012/2/21

#
JJEagle wrote...
@Duta Where does this section go under? Like which Public void (...) does it go under? eatPizza(null); //Or if you want to feed a pizza somewhere else in the world Pizza pizza = //A reference on the pizza you want to feed to the cat ; eatPizza(pizza); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I misunderstood what was meant by manually feeding the cat - ignore that section completely. All you need to do is to change the findPizza method to:
public void findPizza()
{
    Pizza pizza = getOneIntersectingObject(Pizza.class);
    if(pizza != null)
        eatPizza(pizza);
}

public void eatPizza(Pizza pizza)
{
    eat();
    if(pizza != null) getWorld().removeObject(pizza);
    pizzaEaten++;
    howManyEaten();
    energy += 5;
}
khadeidradlinder khadeidradlinder

2013/2/17

#
i have did all the right code but now my friendly cat will not dance after it eats the 3 pizzas. what shall i do?
danpost danpost

2013/2/17

#
Please supply the code for you FriendlyCat class at this point.
You need to login to post a reply.