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

2012/4/12

i dont understand this movement code

wahaj wahaj

2012/4/12

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

/**
 * The ball of the game. It moves and bounces off the walls.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    private int count = 2;
    
    /**
     * Create a ball with random movement.
     */
    public Ball()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = Greenfoot.getRandomNumber(11) - 5;
    }
    
    /**
     * Act. Move and produce smoke.
     */
    public void act() 
    {
        makeSmoke();
        move();
    }
    
    /**
     * Move the ball. Then check whether we've hit a wall.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkWalls();
    }
    
    /**
     * Check whether we've hit one of the walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            deltaY = -deltaY;
        }
    }
    
    /**
     * Put out a puff of smoke (only on every second call).
     */
    private void makeSmoke()
    {
        count--;
        if (count == 0) {
            getWorld().addObject ( new Smoke(), getX(), getY());
            count = 2;
        }
    }
}
this is the code from chapter 8 in the textbook. scenario name is Smoke. im trying to duplicate the effect but using my own code. my methods and the way the code written is different. but im having trouble moving the ball. at line 18 is that a constructor? so far ive only used constructor for the World class so im not sure if Actors have constructors. also what does the code at line 20, 21 do? to my knowledge its just creating random numbers, calculating and then storing the result in fields. the real confusion for me is that when i use the set location method as shown setLocation( getX(), getY() ) the ball "teleports" to the edge but if i use setLocation ( getX() + deltaX, getY() + deltaY() ) the ball actually moves towards the edges. i really dont understand what the fields (deltaX and deltaY ) have to do with the movement of the ball. if anyone could answer my questions and clear up the confusion help would be much appreciated p.s im not at the smoke effect yet. still trying to figure out the movement. so dont worry about that part of the code
ttamasu ttamasu

2012/4/12

#
just think of deltaX as being the velocity in the x direction and the deltaY as being the velocity in the y direction In statement 20, 20. deltaX = Greenfoot.getRandomNumber(11) - 5; all this statement does is assign a random value between -5 and 5 and 21 does the same for delta y so if you have a postive value for x it will go to the right and and negative you will go to the left ]and for y if you have a postive value it will go down and go up if you have a negative value. Now if you are very unlucky you may get deltaX = deltaY = 0 which means the ball doesn't move at all So deltaX is how much the ball moves either to the right(positive) or left (negative per clock 'tick" or cycle in the Act() loop. and deltaY is how much the ball moves either up (negative) or down (positive) per cycle cheers Takashi
wahaj wahaj

2012/4/12

#
right i understand that its generating numbers and what the numbers mean. but why do these numbers give the ball movement in the setLocation method?. if i put the coordinates without these numbers the ball just teleports to that location. and as far as i know these quantities arent vectors so why do they make the ball move?
Duta Duta

2012/4/12

#
setLocation(x, y) does what it says on the tin - it sets the location of that actor to the x and y positions you give. There are also the methods getX() and getY() which give you the current x and y values of the actor. So, say you want to move 5 across from where you are. To do this, you could do the following: setLocation(getX() + 5, getY()); You could imagine this as "Set my x location to where I am currently plus 5, and keep my y the same" Apply this to the above code and it should be make sense
danpost danpost

2012/4/12

#
Each cycle, as mentioned above, the act() methods of the world class and all active objects in the world, in turn, all get a chance to execute. So each time the Ball time to execute comes around, the act() method is executed. The ball class act() method calls a method called 'move()' (which will execute each cycle, since there are no qualifications on it to prevent it from executing in the act() method; that is, no 'if (whatever) move()', just 'move()'). The setLocation in the move() method takes the current location, each time, and adds slight adjustments to the coordinates (deltaX and deltaY) to set the new location; hence, moving a little in some direction. That direction will be constant, as long as deltaX and deltaY are not altered.
wahaj wahaj

2012/4/12

#
hmm ok thanks. it still defies my logic but this will do for now.
You need to login to post a reply.