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/8

#
This was a test I just failed and I don,t have a clue on what I was suppose to do
public class Ball  extends Actor
{
    private int oldX ;
    private int newX ;

    public Ball()
    {
        oldX = 0;
        newX = 0;
    }

    public void act() 
    {

        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;
            
        }

        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()+1, getY());
        }
    }    
}
danpost danpost

2012/3/9

#
I do not see where you store the location of the ball into oldX
oldX = getX();
should be the first statement in your act() method.
Tomc84 Tomc84

2012/3/9

#
Thank you ! I see my new code public class Ball extends Actor { private int oldX ; private int newX ; public Ball() { oldX = 0; newX = 0; } 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; } 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()+1, getY()); } } }
Tomc84 Tomc84

2012/3/10

#
Can the ball be reversed once it hits the right edge?, my attempts have failed ... public class Ball extends Actor { private int oldX ; private int newX ; public Ball() { oldX = 0; newX = 0; } 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; } 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()+1, getY()); } if (getX() == 600-1 ) { //nt deltaX2 = Greenfoot.getRandomNumber(100)-100; //newX = oldX + deltaX2; } // if (oldX newX) // { // setLocation(getX()-1, getY()); // } // if (getX() == 600-1) // { // moveLeftNow(); // } // if (getX() == 600-1) // { // int deltaX2 = Greenfoot.getRandomNumber(100)-100; // newX = deltaX2; // // } // else if (oldX > newX) // { // setLocation(getX()-1, getY()); // } } // public void moveLeftNow() // { // if (Greenfoot.mousePressed(this) )//&& getX() == 600-1 ) // { // int deltaX2 = Greenfoot.getRandomNumber(100)-100; // newX = oldX + deltaX2; // } // // if (getX() == 600-1) // // { // // int deltaX2 = Greenfoot.getRandomNumber(100)-100; // // newX = deltaX2; // // // // } // else if (oldX > newX) // { // setLocation(getX()-1, getY()); // } // } }
danpost danpost

2012/3/10

#
It can, but the coding would depend on exactly what you want to happen. Will the ball still stop at a specific location, or will the ball move a certain amount before stopping?
Tomc84 Tomc84

2012/3/10

#
Stop at the wall, then with a mouse click move to the left
Tomc84 Tomc84

2012/3/10

#
if (newX != oldX) { //moveLeftNow(); Greenfoot.mousePressed(this); int deltaX2 = Greenfoot.getRandomNumber(100); newX = oldX - deltaX2; setLocation(getX() -1, getY()); } this makes it move left in the same way but now the right move is gone
danpost danpost

2012/3/10

#
Just add another instance integer variable to track the direction ('1' for 'to the right', and '-1' for 'to the left') and initialize it to 1. Then for 'newX', use
newX = oldX + deltaX * direction;
and add right after that
if (newX > getWidth() - 1) newX = getWidth() - 1; // to stop at right edge
if (newX < 0) newX = 0; // to stop at left edge
In the setLocation statement, change 'getX() + 1' to 'getX() + direction'. Finally, right after the setLocation statement, add
if (getX() == 0 || getX() == getWidth() - 1) direction = -direction;
danpost danpost

2012/3/10

#
My last post was being written before yours. So the code is in reference to previous code, not that which is in your last post. And I see by your last post that you rightly correctly the if statement
if (newX != oldX)
Using '<' or '>' would not work for one of the directions. Also, the getWidth()s in my last code post should be preceded by getWorld() 'getWorld().getWidth()'
Tomc84 Tomc84

2012/3/10

#
do I just add one int variable? or two? one for (1) and one for (-1) ?
Tomc84 Tomc84

2012/3/10

#
public class Ball extends Actor { private int oldX ; private int newX ; private int direction ; public Ball() { oldX = 0; newX = 0; direction = 1; 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 (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; } } } not sure how to set direction to (1) thought I could do setdirection = 1; but that did not work
danpost danpost

2012/3/11

#
Remove line 11: direction = -1; Change line 26 to: if(oldX != newX) { The initial direction for the ball is toward the right (increasing values of x), so direction needs to start with a value of 1. Line 11 changes (or sets) the value of the variable again, which would be pointless (why set it, and then immediately change it?) The last statement in the class (direction = -direction;) changes the value as needed between 1 and -1. Since the ball will be going both ways (right and left) oldX and newX could be in either order (left to right), so using '<' or '>' will not work in one of the two directions; hence, '!=' is used.
danpost danpost

2012/3/11

#
In the future, either use the second tag ("code") below the reply box and copy/paste your code in the code window, or type ' ' immediately before your code and ' ' immediately after the code. I enclosed the characters that you need to add between single quotes and added spacing between each character because if I were not to do this, it would show like this: or type '
' immediately before your code and '
' immediately after the code.
danpost danpost

2012/3/11

#
Also, I see you forgot to add some code I suggested. Your conditional mousePress code should look like this:
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 < 0) newX = 0;
    if (newX > getWorld().getWidth()) newX = getWorld().getWidth();
}
danpost danpost

2012/3/11

#
Do realize that it is possible that a mousePress on the ball could result in the ball not moving at all. If this is something you wish to avoid change line 2 in the previous post to
int deltaX = Greenfoot.getRandomNumber(75) + 25;
The two numbers can be adjusted to suit your needs, but the sum of the two should add up to the maximum movement wanted (100, in your case).
There are more replies on the next page.
1
2