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

2020/3/21

push actor

Starlord_20 Starlord_20

2020/3/21

#
I want an actor to push another actor in front of him in his current direction when he moves. Is that possible?
danpost danpost

2020/3/21

#
Starlord_20 wrote...
I want an actor to push another actor in front of him in his current direction when he moves. Is that possible?
This and this both show that it is possible. The first one has source code available and can be downloaded so you can check out its coding.
Starlord_20 Starlord_20

2020/3/21

#
I made it like that but it doesn't work:
if(mushroomFront()) {
            Actor actor = (Actor) getOneIntersectingObject(Mushroom.class);
            if(actor != null) {
                actor.setRotation(this.getRotation());
                actor.move(1);
            }
        }
here the code for mushroomFront() :
 boolean mushroomFront() {
        int r = getRotation() / 90;
        return getOneObjectAtOffset((1-r)%2,(2-r)%2,Mushroom.class) != null;
    }
(my World is 25 x 25 cells tall so the boolean works)
danpost danpost

2020/3/21

#
If mushroom is in front, then how would it be intersecting? As is, I do not see how line 2 would ever NOT be null.
Starlord_20 Starlord_20

2020/3/21

#
what could I do instead?
danpost danpost

2020/3/21

#
Starlord_20 wrote...
what could I do instead?
Instead of returning a boolean that just gives the state of something being there, return what is (or isn't -- null) there:
private Mushroom mushroomFront()
{
    int r = getRotation()/90;
    return getOneObjectAtOffset((1-r)%2, (2-r)%2, Mushroom.class);
}
You would then use something like this:
Actor actor = mushroomFront();
if (actor != null) // etc.
Starlord_20 Starlord_20

2020/3/21

#
ok thank you!
You need to login to post a reply.