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

2017/7/3

Why don't appear new objects after some time

Hi, I have the following code and every 2 seconds their should be a new object, but after some time no new objects are created. Could you please explain to me what causes that. Here is the code of the actor class
public class DIRT extends Actor
{

    private int x;
    private int y;
   

    /**
     * Act - do whatever the DIRT wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     * 
     * 
     * 
     */
    public void act() 
    { 
        
        if(RAINBOWPLANET.currentTime>(RAINBOWPLANET.last_dirt+2)){
            RAINBOWPLANET.last_dirt=RAINBOWPLANET.currentTime;

            while(x<=UNICORN.xpos+200){
                x = Greenfoot.getRandomNumber(UNICORN.xpos+300);
            }
            while(y<=UNICORN.ypos-10){
                y = Greenfoot.getRandomNumber(UNICORN.ypos+50);
            }
           
            getWorld().addObject(new DIRT(), x,y);
            
        }    
    }
Here are the lines from the class RAINBOWPLANET
 public static long last_dirt;
// in the constructor of the class
start_time=System.currentTimeMillis()/1000;
last_dirt=start_time;
currentTime=start_time
And finally the code of the UNICORN class:
// in the act method
 xpos=getX();
 ypos=getY();
The problem is especially after the re-location of the unicorn at the beginning of the world if it has reached an edge.
danpost danpost

2017/7/4

#
Line 18 in the DIRT class will never be true until you begin refreshing the value of RAINBOWPLANET.currentTime.
Thanks I have the following line the act method of RAINBOWPLANET currentTime= System.currentTimeMillis()/1000; but it doesn't change much The few objects, which are appearing, are always at the edge of the world after the unicorn has reached one edge and is relocated at the beginning of the world
danpost danpost

2017/7/4

#
The major issue is that you are trying to create dirt from dirt. Dirt really does not do anything on its own -- it just exists. The DIRT class should be stripped of all fields (x and y) and methods (the act method, which is what it currently has). The code in the DIRT act method (or what it does) would be better placed in the RAINBOWPLANET act method.
You need to login to post a reply.