This site requires JavaScript, please enable it in your browser!
Greenfoot back
joseph.stafford2@waldenu.edu
joseph.stafford2@waldenu.edu wrote ...

2012/12/22

Code

What is wrong with this code if ( canSee(Fly.class) ) that it gives me some message "cannot find symbol-method canSee (java.lang. Class<Fly>) .
danpost danpost

2012/12/22

#
Do you not have a method in the class or in a super-class called 'canSee'?
This is what I have public void lookForFly() { if ( canSee(Fly.class) ) { eat(Fly.class); Greenfoot.playSound("slurp.wav"); } Am I missing a step? I am a beginner.
danpost danpost

2012/12/22

#
On your last post, you have shown that you have a method called 'lookForFly' which is missing its closing bracket (you may have missed it when copy/pasting the code). However, you still have not shown that you have the method 'canSee' (or 'eat'). If you do have those as well, you will probably need to post the whole class. Please use the 'code' tag below the 'Post a reply' box and copy/paste your code into the new input window. If you cannot get that to work, you can manually enclose your code with (before) and (after). Do not include the spaces between the characters.
behappy2 behappy2

2012/12/30

#
public void eat()
    {
       Actor food;
       food = getOneObjectAtOffset(0,0, food.class);
       if (food != null)
       {
           World world;
           world = getWorld();
           world.removeObject(food);

    }
this is a whole eat code, 'me' is the actor, 'food' is the eaten item -- remember to add 'eat();' to ur act method thanks
danpost danpost

2012/12/30

#
@behappy2, this 'eat' method has no parameter; while the code joseph.stafford2@waldenu.edu supplied does. The calling statement and the method must match both in name and in the number of parameters and the types of each parameter.
behappy2 behappy2

2012/12/30

#
? in my act i have eat(); then, a new void, eat it works
danpost danpost

2012/12/30

#
Sure, that will work; without a 'lookForFly()'!
behappy2 behappy2

2012/12/30

#
ive got a spider and fly scenario, and i wanted my flies to make a new fly when the met at offset, i could never find the code for that, i tried that above, but inserted the world's add fly code there, no errors but didnt work
behappy2 behappy2

2012/12/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class me here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class me extends Actor
{
    public void act() 
    {
        lookforsmting();
        if (thing != null)
        {
            eat();
        }
    }

    public void lookforsmting()
    {
        Actor thing;
        thing = getOneObjectAtOffset(0,0, thing.class);     
    }

    public void eat()
    {

        World world;
        world = getWorld();
        world.removeObject(thing);

    }
}
would this be better?
danpost danpost

2012/12/30

#
That would not work (or even compile). (1) line 13 does not assign any value to 'thing' (as far as the 'act' method is concerned) (2) the 'lookforsmting()' method is useless as written (it assigns a value to 'thing' which is discarded upon exiting the method) The error you will probably get is 'cannot find variable thing on line 14. To make this code viable: (1) change 'void' on line 20 to 'Actor' (2) insert at line 24
return thing;
(3) change line 13 to
Actor thing = lookforsmting();
(4) change line 16 to
eat(thing);
(5) change line 26 to
public void eat(Actor thing)
However, instead of passing the actor back and forth between methods, I would prefer to code the class like the following:
import greenfoot.*

public class Me extends Actor // it is customary to begin class names with capital letters
{
    public void act()
    {
        eat();
    }

    public void eat()
    {
        Actor thing = getOneIntersectingObject(Thing.class);
        if(thing != null) getWorld().removeObject(thing);
    }
}
Now, if there were other (different actor type) things to eat as well, generalizing the 'eat' method might be in order with refactoring; but as it stands right now, this would be sufficient.
behappy2 behappy2

2012/12/30

#
ok, so put all the lookforsmting() stuff into eat thx
You need to login to post a reply.