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

2023/10/25

need help with moving up and down

Matetity Matetity

2023/10/25

#
My character needs to move up and down and needs to turn when it hits a wall. But it does some weird stuff when it hits the wall, I'm not sure what I'm doing wrong? Here's the code i've done.
public void act() {
      die();
      setLocation(getX(), getY() - 2);
      if (isTouching(wallT.class)) { 
        turn (180);
        setLocation(getX(), getY() + 2);
      }
     }
danpost danpost

2023/10/26

#
Matetity wrote...
My character needs to move up and down and needs to turn when it hits a wall. But it does some weird stuff when it hits the wall, I'm not sure what I'm doing wrong? Here's the code i've done. << Code Omitted >>
You, for one thing, are having it turn around and then move backwards which is in the same moving direction as before. Wall penetration is inevitable. Change the "+" in line 6 to a "-".
Matetity Matetity

2023/10/26

#
danpost wrote...
Matetity wrote...
My character needs to move up and down and needs to turn when it hits a wall. But it does some weird stuff when it hits the wall, I'm not sure what I'm doing wrong? Here's the code i've done. << Code Omitted >>
You, for one thing, are having it turn around and then move backwards which is in the same moving direction as before. Wall penetration is inevitable. Change the "+" in line 6 to a "-".
Right, I already thought it was that, but now it does the same thing as before but it goes thru the wall. I'm very confused on what I did wrong....
Matetity Matetity

2023/10/26

#
To be more clear it hits the wall and then just start turning uncontrolably. I've tried a bunch of different code but it just isn't working, any ideas on why??
Matetity Matetity

2023/10/26

#
This is the whole code of my character:
public class Rocket2 extends Actor
{
    /**
     * Act - do whatever the Rocket2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() {
      die();
      move();
    }
      public void move(){
      
      if (isTouching(Wallie.class)) { 
        turn (180);
        setLocation(getX(), getY() - 2);
      }
      else{
          setLocation(getX(), getY() - 2);
     }
    }
    public Rocket2()
    {    
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 30, image.getHeight() - 60);
        setImage(image);
    }
    public void die()
    {
       if(isTouching(player.class))
       {
         removeTouching(player.class);  
         
        }
    }   
}
danpost danpost

2023/10/26

#
Matetity wrote...
To be more clear it hits the wall and then just start turning uncontrolably. I've tried a bunch of different code but it just isn't working, any ideas on why??
I should have noticed it before. The turn and move methods go together; the setLocation method does not work well with turn. Here, you will need an int direction field (unless your image is rotated to move right to begin with (at 0 rotation). Try:
    private int direction = -1;
    
    private void move() {
        setLocation(getX(), getY()+2*direction);
        if (isTouching(Wallie.class)) {
            turn(180);
            direction = -direction;
            setLocation(getX(), getY()+2*direction);
        }
    }
You could also do it without the int direction field. Like this:
private void move) {
    turn(-90);
    move(2);
    if (isTouching(Wallie.class)) {
        turn(180);
        move(2);
    }
    turn(90);
}
Just takes some extra turning.
Matetity Matetity

2023/11/6

#
Yes, that worked. Thank you so much!!!
You need to login to post a reply.