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

2016/3/11

How do I get my ball to bounce off the sides of the screen and the paddles?

tn13673 tn13673

2016/3/11

#
mport greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ball extends Actor
  
    
{
    public int xspeed = 4;
    public int yspeed = 4;
    public static int ballX, ballY;
    /**     * Act - do whatever the paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         
        setLocation(getX() + xspeed, getY()+yspeed);
        bounce();
        ballX = getX ();
        ballY = getY ();
    }

    public void movement()
    {
        int worldWidth = getWorld().getWidth();
        int worldHeight = getWorld().getHeight();
        int spriteWidth = getImage().getWidth()/2;
        int spriteHieght = getImage().getHeight()/2;
        if(getX() <= spriteWidth)
        xspeed = xspeed * -1;
        
        if(getX() >= spriteWidth - spriteWidth)
         xspeed = xspeed * -1;
        
        if(getY() <= spriteHieght)
         yspeed = yspeed * -1;
         
        if(getY() <= spriteHieght)
         yspeed = yspeed * -1;
        
        bounce ();
    }
    
    
      public void bounce ()
   {
        Actor paddle = getOneIntersectingObject (mypaddle.class) ;
        if(paddle != null)
        {
            yspeed = yspeed * -1;
           
        }
        
        
   


}
}
danpost danpost

2016/3/11

#
I does not look like you changed anything in your 'movement' method as discussed in your previous discussion thread. You should have continued that thread, not started a new one. One more thing that I can add from the additional code given is that the 'movement' method is not being called from anywhere within the class (so how can it even execute when there is no link to running code). Even the name of the method is misleading -- it deals with the direction to move (or edge collision checking), not with the movement itself.
tn13673 tn13673

2016/3/16

#
I fixed everything, my teacher finally got around to helping me.
You need to login to post a reply.