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

2013/1/17

Problem with deleting an object at a certain time

zakariaonlyy zakariaonlyy

2013/1/17

#
Good evening, I have a problem with a certain code. I am trying to say that if the elapsedTime is Higher then 3000 it has to delete the object grindzak. But it never comes in the code that says : remove.Object (this);. I tried to do a system.out.print of the elapsedTime but it never gets higher then 150. I think that has something to do with the act method because an act is happenning every 143Milliseconds, so it never gets higher then 150. I hope you can help me out what i want it to do is the delete the object grindzak after 3 seconds. the code is :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class grindzak here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class grindzak extends Burger
{
    /**
     * Act - do whatever the grindzak wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private long initialTime = System.currentTimeMillis(); 
    
    public void act() 
    {
        Actor checkWater = getOneIntersectingObject(Watervloed.class);
        
        long currentTime = System.currentTimeMillis();
        long elapsedTime = currentTime - initialTime; 
        
        if(checkWater != null)
        {
             getWorld().removeObject(checkWater);  
             if(elapsedTime > 1000)
             {
                initialTime = currentTime;  
                getWorld().removeObject(this);
             } 
        }
    }    
}
danpost danpost

2013/1/17

#
Well, what the code says right now is "if intersecting a Watervloed object AND if the elapsed time has expired, then remove 'this' from the world (and reset timer)".
zakariaonlyy zakariaonlyy

2013/1/18

#
well even if the time has elapsed it stays very low, like at 143milliseconds, it will never reach the 3 seconds any idea/hints how to fix it?
danpost danpost

2013/1/18

#
Comment out lines 25, 26, and 33 from above and test it then. Report back as to results.
actinium actinium

2013/1/18

#
Instead of using system time, i would use 2 instance variables, rate and rateCount. Set rateCount to zero and rate to say 300. Then increment rateCount in the act method and test wheter rateCount > rate, if so do whatever you want to do and then reset rateCount.
zakariaonlyy zakariaonlyy

2013/1/18

#
It worked perfecty thank you Dan :) Actinium also thanks for your response mate
You need to login to post a reply.