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

2012/3/8

help plz

1
2
Tomc84 Tomc84

2012/3/11

#
oh ok, so this is what I have. it worked one way then did not come bake the other way and it it not running off the mouse click... I know I did some thing wrong just not sure what that is.
public class Ball  extends Actor
{
    private int oldX ;
    private int newX ;
    private int direction ;
    
    public Ball()
    {
        oldX = 0;
        newX = 0;
        direction = 1;
        
        
    }

    public void act() 
    {
        oldX= getX();
        if (Greenfoot.mousePressed(this)){
            int deltaX = Greenfoot.getRandomNumber(100);
            // You need to store the store the current location of the ball at oldX
            // that newX can be obtained based on it.

            newX = oldX + deltaX * direction;
            if (newX > getWorld().getWidth()-1){
                newX = getWorld().getWidth() -1;
            }
            if (newX < 0){
                newX =0;
            }
        }

        if (oldX != newX){
            // The ball keeps moving towards newX only when it is not there yet.
            // As soon as it reaches newX, it stops moving.

            setLocation(getX()+direction, getY());
        }
        if (getX() == 0 || getX() == getWorld().getWidth() -1)
        {
            direction = -direction;
        }
    } 
}
danpost danpost

2012/3/11

#
So, is it working now? BTW, the code looks good.
Tomc84 Tomc84

2012/3/11

#
It looks good thanks to you! I was failing to get it done. it works for the most part, it moves when I hit run though and when it reaches 0 on the left it stops there
Tomc84 Tomc84

2012/3/11

#
I think maybe I can use this
  public void act() 
    {
        oldX= getX();
        if (Greenfoot.mousePressed(this)){
            int deltaX = Greenfoot.getRandomNumber(75) +25 ;
            // You need to store the store the current location of the ball at oldX
            // that newX can be obtained based on it.

            newX = oldX + deltaX * direction;
            if (newX > getWorld().getWidth()-1){
                newX = getWorld().getWidth() -1;
            }
            if (newX < 0){
                newX =0;
            }
        }

        if (oldX != newX){
            // The ball keeps moving towards newX only when it is not there yet.
            // As soon as it reaches newX, it stops moving.

            setLocation(getX()+direction, getY());
        }
        if ( getX() == getWorld().getWidth() -1)
        {
            direction = -direction;
        }
        if (getX() == 0)
        {
            direction = direction *(-1);
        }
    } 
}
danpost danpost

2012/3/11

#
There are two ways, that I can think of at the moment, to prevent it from moving when you hit 'Run'. (1) The cheap way: Change lines 9 and 10 so that the values are set to the actual X value that you are placing the ball in the world at. If you decide to initialize the ball at a different location, this would have to be adjusted. (2) The proper way: Remove lines 9 and 10, and add this method
public void addedToWorld(World w)
{
    newX = getX();
    oldX = getX();
}
This way would not need adjusting if the ball was initialized at a different location.
danpost danpost

2012/3/11

#
I do not see how this is neccessary, but if you are still having problems with the ball stopping at the left and not being able to move it back to the right, try tightening up the code a bit: Adjust lines 24 through 31 to
if (getX() == getWorld().getWidth() - 1 && direction == 1) direction = -1;
if (getX() == 0 && direction == -1) direction = 1;
danpost danpost

2012/3/11

#
I see the problem, now! FORGET the last post! Remove lines 24 through 31; they are not in the right location. Your code should be:
if (oldX != newX){
    // The ball keeps moving towards newX only when it is not there yet.
    // As soon as it reaches newX, it stops moving.
    setLocation(getX()+direction, getY());
    if (getX() == 0 || getX() == getWorld().getWidth() - 1) direction = -direction;
}
You need to login to post a reply.
1
2