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

2016/3/9

Make an object go away after a certain time

Bassusour Bassusour

2016/3/9

#
As the title says, I want an object to disappear after a certain time, or having traveled a certain distance.
danpost danpost

2016/3/9

#
Bassusour wrote...
As the title says, I want an object to disappear after a certain time, or having traveled a certain distance.
What code do you have for the actor so far? What have you tried as far as implementing the required behavior?
Bassusour Bassusour

2016/3/9

#
I havn't reached far, so this is what I've done
public void forsvind()
    {
        if (//the method to measure time or travel)
        {
            removeObject(ND.class);
        }
    }
danpost danpost

2016/3/9

#
Bassusour wrote...
I havn't reached far, so this is what I've done
public void forsvind()
{
    if (//the method to measure time or travel)
    {
        removeObject(ND.class);
    }
}
What class is this code in? The timer field and the timer control (method that has the timer count and remove the object) should be in the class of the object to be removed after so much time. As coded, it appears to be in your subclass of World (per line 5).
Bassusour Bassusour

2016/3/9

#
My bad, I was just lazy, and did not think about putting the getWorld in before the method. The class this code is in, is a subclass of animals (yes, from the vrab world, which I've added a lot of other things to).
public void forsvind()
{
    if (//the method to measure time or travel)
    {
        getWorld.removeObject(ND.class);
    }
}
danpost danpost

2016/3/9

#
Still lazy, I see -- the method call 'getWorld' needs to be followed by a set of round brackets to indicate it is a method and not a field. Laziness has no place in programming -- it is a precise and detailed art. You need to introduce an instance int timer field that is incremented each act cycle and when it reaches a predefined value, the object is removed.
Bassusour Bassusour

2016/3/10

#
Thanks for replying. I will no longer do it the lazy way, and thanks for pointing it out. But how do I introduce an instance timer field? I have searched around the internet, but i don't understand it. Would you be kind and explain with an example how to implement this timer?
Super_Hippo Super_Hippo

2016/3/10

#
Bassusour wrote...
But how do I introduce an instance timer field?
private int timer = 0;
And then, to increment it in the act-method:
timer++;
You need to login to post a reply.