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

2016/11/3

Delay a single object

Idadorable Idadorable

2016/11/3

#
I'm doing a computer game for school. I have some objects, that one needs to click on before they reach a a certain x-coordinate. When you click on the object it is removed. I want to change the object's image just before it disappears, but "delay" will pause the whole project, which is not what I intend to do. My image change is working fine and my removal of the object is as well.
    public void removeCat()
    {
        if(Greenfoot.mouseClicked(this)){
            setImage("cat"+imageNumber+"g.png");
            Greenfoot.delay(20);
            getWorld().removeObject(this);
        }
    }
Does anyone have any suggestions as to how I can show the picture for, say, 0,2 seconds, and afterwards remove the object?
danpost danpost

2016/11/3

#
I think you can just add an instance int field to time the delay for removal:
// with instance field
private int removeIn;

// on mouse click
setImage("cat"_imageNumber+"g.png");
removeIn = 20;

// added code in act or a method it calls (could be added to the end of the 'removeCat' method)
if (removeIn > 0)
{
    if (--removeIn == 0)
    {
        getWorld().removeObject(this);
    }
}
Idadorable Idadorable

2016/11/3

#
Thank you!
danpost danpost

2016/11/3

#
Idadorable wrote...
Thank you!
I guess that the 'if' statements can be combined as follows:
if (removeIn > 0 && --removeIn == 0) getWorld().removeObject(this);
You need to login to post a reply.