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

2015/12/29

remove instructions

Ben3 Ben3

2015/12/29

#
i want my instructions for my game to disappear either when a point is scored or after 3 seconds but not sure what the code is please help
Super_Hippo Super_Hippo

2015/12/29

#
I guess the instructions are displayed as the image of an actor. (Later in the code as the 'Instruction' class. If it has an other name, adapt that.) For the time: (if you do not need it exactly 3 seconds)
//Instruction class

private int time = 180; //approx. 3 seconds with speed slider in the middle

public void act()
{
    time--;
    if (time==0)
    {
        removeObject(this);
    }
}
When a point is scored, you should have a line of code to remove the instructions. If this happens when an actor does something (e.g. collecting), then it will be in an actor subclass:
//actor which scores
if (/*touching item*/)
{
    //collect item
    //add score
    Actor i = (Actor) getWorld().getObjects(Instruction.class).get(0);
    if (i != null) getWorld().removeObject(i);
}
Ben3 Ben3

2015/12/29

#
where does all this code go? for the three second one where in instructions class does 'private int time = 180;' go? and the second bit is that a whole new section? I'm kind of new to greenfoot so a bit confused
Super_Hippo Super_Hippo

2015/12/29

#
The 'private int time = 180;' has to be outside methods (top of the class code). The second bit is not in the Instruction class. It is in the class of the actor which gets the point (probably the main actor).
Ben3 Ben3

2015/12/29

#
does the second bit go in the act method because there is already 'public void act' and also what do I type where it says 'this' in brackets after remove objects because it says cannot find variable. ive copied and pasted the code but not sure what to do now because its not working
Super_Hippo Super_Hippo

2015/12/30

#
Yes, looks like I didn't really think when I wrote it... it should be:
getWorld().removeObject(this);
That is the same act method. So if you already have something else in it, keep it in the beginning and add the rest.
danpost danpost

2015/12/30

#
NVM. (I was looking at the wrong code) To clarify, the correction was for line 10 above (in the Instruction class).
You need to login to post a reply.