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

2017/4/17

Breakout - Bouncing Ball sticking on the floor and the right side

Phirippu Phirippu

2017/4/17

#
Hi, I have to make a Breakout-Game for a schoolproject. But I have the problem that my ball is sticking onthe right side and on the floor (on the floor he has to get removed). On the left and the top he is bouncing... I hope you can help me.
public class Ball extends Actor
{
            private int dx;
            private int dy;
        public Ball() {
            dx = 10 - Greenfoot.getRandomNumber(21);
            dy = -5 - Greenfoot.getRandomNumber(6);
        }
        /*
         * Check the 4 sides
         */
    private void pruefeKontaktRandRechts() {
        if (getX() >= 626) {
            dx = -dx;
        } 
    }
    
    private void pruefeKontaktRandLinks() {
        if (getX() <= 0 ){
            dx = -dx;
        }
    }
    
    private void pruefeKontaktRandUnten() {
        if (getY() >= 466) {
            getWorld().removeObject(this);
        }
    }
    
    private void pruefeKontaktRandOben() {
        if(getY() <= 0) {
            dy = -dy;
        }
    }
    

    
    public void act()
    {
         pruefeKontaktRandRechts();
         pruefeKontaktRandLinks();
         pruefeKontaktRandUnten();
         pruefeKontaktRandOben();
         setLocation (getX() + dx, getY() + dy);
        }
    
Super_Hippo Super_Hippo

2017/4/17

#
Change line 13 to:
if (getX() >= getWorld().getWidth()-1)
and line 25 to:
if (getY() >= getWorld().getHeight()-1)
Phirippu Phirippu

2017/4/18

#
Its is working now!!! Thank you
You need to login to post a reply.