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

2013/9/9

how to make the asteroid fall in one way?

mateenozil mateenozil

2013/9/9

#
Hello, On tutorial 6, i try to make the asteroid to move downward instead of sideways and i want the asteroid to randomly fall in any X-coordinate. The problem is, when i put
setLocation(Greenfoot.getRandomNumber(600), ypos);
the asteroid did not move downward like a rainfall... but instead, it move sideways vigorously and slowly move downward. My question is how to make the asteroid to move one way from the top to the bottom in any X-coordinate?
davmac davmac

2013/9/9

#
Can you post all the code that you have for the Asteroid class?
danpost danpost

2013/9/9

#
Your first argument in the setLocation statement given (Greenfoot.getRandomNumber(600)) is being executed each act cycle causing a continuous changing in the x coordinate of the object (the vigorous sideways movement). Using a random number is alright when first adding the object to the world, but not when moving the object in the world (unless you really want that kind of movement).
mateenozil mateenozil

2013/9/9

#
here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * An Asteroid that flies across the screen.
 */
public class Asteroid  extends Actor
{
   
    /**
     * Act - do whatever the Asteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        int ypos = getY()+1; 
        setLocation(Greenfoot.getRandomNumber(600), ypos);
        
        if (ypos >= getWorld().getHeight()) {
            getWorld().removeObject(this);
        }
    }    
}
well, I want it to move downward only and spawn at the top from any X-coordinate
danpost danpost

2013/9/9

#
You should add any new asteroid into the world using 'getRandomNumber' within the 'addObject' statement, but should refrain from using 'getRandomNumber' (at least, in your case) in the 'setLocation' statement.
davmac davmac

2013/9/9

#
As danpost says, you shouldn't choose a random X co-ordinate every time the asteroid acts; then it will jump around as it moves down. What you should do instead is to choose a random coordinate when you place the asteroid in the world (which is done in the world class, if I remember correctly).
You need to login to post a reply.