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

2014/2/26

Bouncing ball

Abomb1126 Abomb1126

2014/2/26

#
This, I believe should be a very simple solution, but I really cannont find the answer! I have a Ball class that detects where it's Y var is. If it's less than or equal to 500 (or higher that on the screen) move(speed) (speed is defined earlier as 0) and increment speed, this will give the illusion of acceleration due to gravity. Now for the bouncing part, whenever the above is not true (or goes above 500), set the y to 500, not a noticable movement. (this is so the ball should not get stuck, and the ball would continue moving). It would also make the speed variable the opposite. So in theory, when the speed is negative, and the conditional is true, the ball should move the opposite direction, and, since it's adding 1 every time, should take the same amount of time to bounce up as well. The problem I'm having is that the ball does not want to bounce. It hits the 501 mark, then since it is not true, it then goes to 500, and stays there. Can anyone enlighten me why the ball doesn't even want to move after it is true? Thanks! Ball Class:
import 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 speed = 0;
    /**
     * 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() 
    {
        setRotation(90);
        if(getY()<500){
            move(speed);
            speed++;
        }else{
            setLocation(getX(),500);
            speed -= speed*2;
        }
        
    }    
}
danpost danpost

2014/2/26

#
Line 19 restricts movement to only when the y value is less than 500.
You need to login to post a reply.