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

2016/4/26

Add an object, and remove an object after a while

carter carter

2016/4/26

#
    public void displayCorrect()
    {
        Correct correct = new Correct();
        getWorld().addObject(correct, 289, 190);
       getWorld().removeObjects(getWorld().getObjects(Correct.class));
        
    }
I want there to be a delay between the adding of the object and the removal of the object, but I don't know how. Thank you for your help
danpost danpost

2016/4/27

#
You can use a 'delay' statement to have the actor show for a short time. after line 4, insert the following line:
Greenfoot.delay(40);
I think this is what I used in the given code on your other discussion thread.
carter carter

2016/4/27

#
I read that when you do the delay, it stops the whole program, and if that is true, I have other actions going on at this time
carter carter

2016/4/27

#
By the way, thanks for all your help and understanding that I am very new to this.
danpost danpost

2016/4/27

#
carter wrote...
I read that when you do the delay, it stops the whole program, and if that is true, I have other actions going on at this time
It does. If that is not an option, the you need a reference field for the Correct object and an int field to count out some act cycles before removing it:
/** in Cards class */

// instance fields
private Actor correct;
private int correctActs;

// when creating and adding into world
correct = new Correct();
getWorld().addObject(correct, 289, 190);
correctActs = 120;

// in act or method it calls (test for removal of Correct object)
if (correctActs > 0)
{
    correctActs--;
    if (correctActs == 0) getWorld().removeObject(correct);
}
You need to login to post a reply.