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

2018/1/22

Making some infos to appear again after they are closed

kidduts kidduts

2018/1/22

#
Hi, im making an educational game for a contest and i need some help.
public boolean f=false;
public void act(){
if(!f)
q11();
}
public boolean q11(){
    Actor q=getOneIntersectingObject(q1.class);
    if(q!=null){
         if(Greenfoot.isKeyDown("f")){
        f=false;
getWorld().addObject(new text1(),getX(),getY());}}
    return f;
    }
As you already know, when the main char collides with the q Actor and when i press f, it will appear a new object named text1 which contains some text. The problem is that i don't know how to make it reappear when i press f again, i tried to move the boolean variable inside the method or make it true again after or before the f=false but it didnt work as expected. I made this boolean method because i tried to make it appear only once if i press a key and it worked, i you got any idea how to make it better im open for any ideas. Btw im a begginer and its my first time working on this program so dont take me too hard :D. Thanks!
danpost danpost

2018/1/22

#
Since 'f' is 'false' when you add a text1 object into the world, it would make sense to make it true at line 10 when adding it into the world (so more are not added). However, you need a deterministic way to reset 'f' back to 'false'.. Probably the easiest way it to check for the text1 object being in the world. This would actually remove the need for the 'f' field altogether:
public void act()
{
    if (Greenfoot.isKeyDown("f")
        && getOneIntersectingObject(q1.class) != null
        && getWorld().getObjects(text1.class).isEmpty())
            getWorld().addObject(new text1(), getX(), getY());
}
kidduts kidduts

2018/1/23

#
Oh ok, ok i get it. I dont know a good way to reset the "f" back to false so i tryed your code and it worked well, i have also modified it to script a quest in my game. Thank you so much, i dont know why "f=false" is there, it was supposed to be true tho :D, as you said. Thanks again, it helped me so much.
You need to login to post a reply.