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

2014/12/4

isTouching(Class) - in Greenfoot 2.2?

HaselnuesseTo HaselnuesseTo

2014/12/4

#
Hey! We're working in school only with G2.2 and not the latest G2.3.0 because we have to learn some elementary commands in Java. So how to implementate "isTouching(Class)" in Greenfoot 2.2? Next tuesday we'll write a test about it and i can use every command which i find in the internet, but i have to know how this command works :) - Now please give me a tip ;) THANK YOU!!!
danpost danpost

2014/12/4

#
Actually, the latest is G2.4.0. The Animal class that comes with G2.2 as an importable class has a 'canSee' method which is synonymous with the 'isTouching' method and also a 'eat' method with is synonymous with the 'removeTouching' method. You can copy/paste those methods into the classes you need them in (and even change their names accordingly, if you wish).
HaselnuesseTo HaselnuesseTo

2014/12/4

#
danpost wrote...
You can copy/paste those methods into the classes you need them in (and even change their names accordingly, if you wish).
Can you give me an example code which would using for this project?
danpost danpost

2014/12/4

#
These are how the methods were in the Animal class of my G2.3.0 USB download:
/**
 * 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);
    }
}
You need to login to post a reply.