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

2019/6/3

Help with walls

jrome jrome

2019/6/3

#
So I'm trying to make a game where the character has to navigate a maze, but I'm pretty new to Greenfoot and I can't figure out how to keep him from going through walls. Is there a way other than the turn(180)?
Super_Hippo Super_Hippo

2019/6/3

#
A very common way is to move, then check if it is touching a wall and if so, move back to where it came from. (Since the actor is not touching a wall at the end of its act method, it is never visually touching a wall.) You can also do this separately for the horizontal and vertical direction.
jrome jrome

2019/6/3

#
Super_Hippo wrote...
You can also do this separately for the horizontal and vertical direction.
How do you get vertical and horizontal?
Super_Hippo Super_Hippo

2019/6/3

#
if you move like this, then dx is the horizontal and dy the vertical movement:
int dx=0, dy=0;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
if (dx != 0 || dy != 0) setLocation(getX()+dx, getY()+dy);
jrome jrome

2019/6/4

#
Is there a way to detect what direction the object you are in contact with is from the character?
danpost danpost

2019/6/4

#
jrome wrote...
Is there a way to detect what direction the object you are in contact with is from the character?
The character's direction of movement can be used to determine that. If character had moved up (dy < 0) then any obstacle encountered would be above the character.
You need to login to post a reply.