How do you make an actor bounce off the world edge in a Pong Game?

12 views
Skip to first unread message

Jeff

unread,
Apr 21, 2015, 3:00:56 PM4/21/15
to greenfoot-dis...@googlegroups.com
Copy and paste the following code.

    public void act()
    {
        move(10);
        bounceOffPaddles();
        ifOnEdgeBounce();
    }
   public void ifOnEdgeBounce()
    {
    int x = getX();
    int y = getY();
    if (x >=getWorld().getWidth() -1) 
    {
        setRotation (180 - getRotation());
    }
    if (x < 1) 
    {
        setRotation (180-getRotation());
    }
    if (y >= getWorld().getHeight() -1)
    {
         setRotation (360-getRotation());
        }
    if (y<1) 
    {
        setRotation (360-getRotation());
    }
    setLocation(x,y);

        
}
Reply all
Reply to author
Forward
0 new messages