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

2018/6/3

I'm trying to make my class not go through walls.

Lev Lev

2018/6/3

#
I've figured out how to make my class (a rocket) not go through walls when pressing left, right, and up, my problem is when I press the "down" key, the rocket will pass through the wall and move to the nearest open space. Any help would be appreciated, Thank you!
   public void checkKeyPressed()
{
    setRotation(90);
    if (Greenfoot.isKeyDown("up"))
    {move(-1); }
    if (canSee(Wall.class))
            {
                move(1);
            }
    if (Greenfoot.isKeyDown("down"))
    {move(1); } 
    setRotation(0);
    if (canSee(Wall.class))
            {
                move(-1);
            }
    if (Greenfoot.isKeyDown("left"))
    {move(-1); }
    if (canSee(Wall.class))
            {
                move(1);
            }
    if (Greenfoot.isKeyDown("right"))
    {move(1); }
    if (canSee(Wall.class))
            {
                move(-1);
            }
  }
  
  }
danpost danpost

2018/6/3

#
Lev wrote...
I've figured out how to make my class (a rocket) not go through walls when pressing left, right, and up, my problem is when I press the "down" key, the rocket will pass through the wall and move to the nearest open space. << Code Omitted >>
You are setting the rotation back to zero too soon (see line 12).
Lev Lev

2018/6/3

#
Then where should I move line 12 to?
danpost danpost

2018/6/3

#
Lev wrote...
Then where should I move line 12 to?
Down to the next logical place in the code.
Lev Lev

2018/6/3

#
I figured it out! Thank you for not giving me the answer xD
You need to login to post a reply.