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

2015/11/23

Please help, cannot get Actor to detect other object

Lamoose Lamoose

2015/11/23

#
Hello, I am new to Greenfoot, and coding altogether, and struggling to use the software. I'm trying to get an actor to detect an object and then stop, but it is detecting nothing. Having completed only the Greenfoot tutorials I've already used up what limited codes I know to detect another actor so if anyone could offer a solution to a noobie I'd very much appreciate Here's what I tried
    public void Stop()
    {
     Actor barrier;
        barrier = getOneObjectAtOffset(0, 0, barrier.class);
        if (barrier != null)
        {
            move(0);
        }
    }
Super_Hippo Super_Hippo

2015/11/23

#
move(0) doesn't do anything. So how do you know that it doesn't work?
Lamoose Lamoose

2015/11/23

#
I've figured it out partly now regardless, if anyone wants to see what I did and help improve on a somewhat messy solution please ask
Super_Hippo Super_Hippo

2015/11/23

#
That's what this discussion forum is for. Feel free to ask for it.
Lamoose Lamoose

2015/11/23

#
I'll attach what I did actually, for anyone else looking for a similar fix
    private boolean barrier_detector()
    {
        Actor barrier;
        boolean the_truth;
        barrier=getOneObjectAtOffset(0,0,barrier.class);
        if (barrier!=null)
        {
            the_truth=true;
        }
        else
        {
            the_truth=false;
        }
        return the_truth;
danpost danpost

2015/11/23

#
That seems like a lot of code for a method that does not do to much -- and you still have to call the method:
if (barrier_detector())
where you could just use:
if (getOneObjectAtOffset(0, 0, barrier.class) != null)
With this last line, the method can be removed altogether.
You need to login to post a reply.