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

2017/2/9

Edge Collision Detection not working

JollyMonSam JollyMonSam

2017/2/9

#
public class Ball extends Actor
{
    Player_1 p1;
    Player_2 p2;
    MyWorld world;
    
    int x;
    int y;
    int ySpeed;
    int xSpeed;
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        x = getX();
        y = getY();
        xSpeed = 5;
        ySpeed = 5;
        
        setLocation(x + xSpeed, y + ySpeed);
        
        if(getX() < 10 || getX() > getWorld().getWidth() - 10) {
            xSpeed = -xSpeed;
        }
        if(getY() < 10 || getY() > getWorld().getHeight() - 10) {
            ySpeed = -ySpeed;
        }
    }
}
By my reasoning, this should work perfectly, but it isn't. Could I have some help please?
Super_Hippo Super_Hippo

2017/2/9

#
Try to remove lines 19 and 20 and set them to the variables to 5 in lines 9 and 10. Lines 24-28 change xSpeed and ySpeed if the Ball is at the edge but these variables are not used before they are set to 5 again.
JollyMonSam JollyMonSam

2017/2/9

#
Super_Hippo wrote...
Try to remove lines 19 and 20 and set them to the variables to 5 in lines 9 and 10. Lines 24-28 change xSpeed and ySpeed if the Ball is at the edge but these variables are not used before they are set to 5 again.
Thank-you. All I had to do is comment out lines 19 and 20 and then declare
int xSpeed = 5
and
int ySpeed = 5
at lines 9 and 10.
You need to login to post a reply.