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

2020/1/10

get actor in front

Starlord_20 Starlord_20

2020/1/10

#
is it possible somehow, to remove an actor in front inside of another actors act() method?
danpost danpost

2020/1/10

#
Starlord_20 wrote...
is it possible somehow, to remove an actor in front inside of another actors act() method?
By "in front", do you mean drawn on top of this actor?
Starlord_20 Starlord_20

2020/1/10

#
i mean his rotation (the world has only 10x10 pixels so there are only 4 directions)
danpost danpost

2020/1/10

#
Starlord_20 wrote...
i mean his rotation (the world has only 10x10 pixels so there are only 4 directions)
Rotation can be converted to horizontal and vertical vectors:
int r = getRotation()/90;
int dx = (1-r)%2;
int dy = (2-r)%2;
The space in front would then be:
int frontX = getX()+dx;
int frontY = getY()+dy;
If the space exists:
if (frontX >= 0 && frontX < 10 && frontY >= 0 && frontY < 10)
You can get what is in front with:
Actor actorInFront = getOneObjectAtOffset(dx, dy, Actor.class);
If an actor IS in front, you can remove it with:
if (actorInFront != null) getWorld().removeObject(actorInFront);
Starlord_20 Starlord_20

2020/1/10

#
Thank you!
You need to login to post a reply.