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

2014/3/21

Bouncing off Walls Help?

jacobgalvan23 jacobgalvan23

2014/3/21

#
Hi, I'm trying to create a world where the object moves and when it hits the edge it bounces back into the opposite direction. I figured out how to make it move to the other side but not how to bounce it off. Here's my code so far-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  public void act()
    {
        move(+12);
        if(getX() == (getWorld().getWidth()-1))
        {
           setLocation(0, getY());
        }
        else if(getX() == 0)
        {
           setLocation(getWorld().getWidth(), getY());
        }     
       
        if(getY() == (getWorld().getHeight()-1))
        {
           setLocation(getX(), 0);
        }
        else if(getY() == 0)
        {
           setLocation(getX(), getWorld().getHeight()); 
        }
    }   
}
danpost danpost

2014/3/21

#
To bounce, you need to adjust the rotation of the object instead of its location.
You need to login to post a reply.