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

2012/6/15

Making a the Bal move with a variable speed that changes when it hits something

bakkie bakkie

2012/6/15

#
public int xVel = Greenfoot.getRandomNumber(3) + 1; public int yVel = Greenfoot.getRandomNumber(3) + 1; public void act(){ if (getY() <= 0) { yVel = yVel * -1; } if (getX() <= 25 || getX() >= getWorld().getWidth()-25) { xVel = xVel * -1; } } public void move() { setLocation(getX() + xVel, getY() + yVel); } public void moveup(){ setLocation(getX() + xVel, getY() - yVel); } how do you make the speed variable with this code i have that it bounces with the edges of the world but not that the speed changes with each bounce. can you help me?
MatheMagician MatheMagician

2012/6/15

#
public int xVel = Greenfoot.getRandomNumber(3) + 1;
    public int yVel = Greenfoot.getRandomNumber(3) + 1;
    public void act(){
        if (getY() <= 25 || getY() >= getWorld().getHeight()-25)
        {
            yVel = yVel * -1;         
            yVel = (Greenfoot.getRandomNumber(3) + 1)/(yVel/Math.abs(yVel));
        }
        if (getX() <= 25 || getX() >= getWorld().getWidth()-25)
        {
            xVel = xVel * -1;   
            xVel = (Greenfoot.getRandomNumber(3) + 1)/(xVel/Math.abs(xVel));
        } 
        move();
    }
    public void move()
    {
        setLocation(getX() + xVel, getY() + yVel);
    } 
The section yVel/Math.abs(yVel) figures out the direction you want to be going in, keeping the ball from sliding across the wall
You need to login to post a reply.