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

2012/11/29

Explain these codes plz!

hkrhässleholm hkrhässleholm

2012/11/29

#
public void move(){
        if(getX() == getWorld().getWidth()-1){
            speedX = -speedX;
        }
        if(getY() == getWorld().getHeight()-1){
            Greenfoot.stop();
        }
        if(getX() == 0){
            speedX = -speedX;
        }
        if(getY() == 0){
             speedY = -speedY;
        }
        setLocation(getX()+speedX,getY()+speedY);
Hi friends, these codes are from a "ball bouncing" game. And these were written inside the move method of the class "ball". Please explain from the first line of if statement. Specially why in this line we need to subtract 1?
davmac davmac

2012/11/30

#
If the width of the world is 500, say, then the possible x coordinates are 0,1,2...499. Because you start counting at 0, there are 500 different values, but the largest one is 499, which is the width minus one.
You need to login to post a reply.