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

2012/11/18

How to let Greenfoot wait?

1
2
3
Sl3nDeRm4n Sl3nDeRm4n

2012/11/18

#
Hi, sorry but I have another question: I want Greenfoot to do nothing until a Key is pressed. How can i realise it? Thx for help:)
groengeel groengeel

2012/11/18

#
Have you looked into the wait method?
danpost danpost

2012/11/18

#
@groengeel, I would think that using the 'wait' command would prevent your scenario from catching any keystrokes until after the 'wait' was completed. @Sl3nDeRm4n, what is triggering the pause to catch a keystroke? are you like on a header screen? or, maybe at a menu? do you already have actors that automatically move in the world, or do you change world to perform the check for keystroke input? Giving appropriate information that deals with your situation goes a long way in providing the best answers possible.
Sl3nDeRm4n Sl3nDeRm4n

2012/11/19

#
i have actors that move and if one actor intersects a specifial other one it creates a new object in the first world. Then it opens a new world. I want to wait Greenfoot to wait with opening the new world until the spacebar is pressed. Could it work like this?
Key k1 = "space";
while(getKey() != k1){

}
Gevater_Tod4711 Gevater_Tod4711

2012/11/19

#
You should use a boolean to know when the first object intersects the other (or what ever should be the condition to add the new world). If this condition is meet the boolean should be true until there is the new world added. Then do this in your act method (of the world or where ever your setWorld is now):
//in your act method
if (theBooleanForTheWorldChangingCondition) {
    if (Greenfoot.isKeyDown("space")) {
        theBooleanForTheWorldChangingCondition = false; //you should use a shorter name I think;
        setWorld(theOtherWorld);//the command you used before to change the world;
    }
}
danpost danpost

2012/11/19

#
Again, with that code you are hogging up all the CPU time while waiting for the keypress. Not a good programming practice. In your initial world, do any of your actors move on their own? or only by user control? If you are not worried about actors that move programatically roaming around, doing whatever they do, then you can code it right there in the class of the actor you determined the intersecting state in.
// add this boolean to the class
boolean waitingForSpaceKey = false; // to be used as a flag
// in the 'if' block that determines intersection
waitingForSpaceKey = true; // sets flag
// in the 'act' method
if (waitingForSpaceKey) // catches flag
{
    if (Greenfoot.isKeyDown("space")) // check for 'space'
    {
        waitingForSpaceKey = false; // clears flag
        Greenfoot.setWorld(new WorldName()); // changes world
    }
    return; // just waiting for keypress
}
This should be one for the first things in your 'act' method (or you can place this code within a seperate method and have it be one of the first method calls in your 'act' method).
danpost danpost

2012/11/19

#
I would like to know what the purpose is of waiting for the spacekey to be pressed before changing worlds. I would have to think it is in response to some message or something, but the likelihood of that would seem pretty slim. Also, I do not know what the new world is for (another level, a menu, something else).
Sl3nDeRm4n Sl3nDeRm4n

2012/11/19

#
Does it also work if my 'if' block isnt inthe class flag? or should i put the boolen just into the class of the intersecting object? And i have actors that move programmically around but its no problem if they stop
Sl3nDeRm4n Sl3nDeRm4n

2012/11/19

#
Im programming agame and if the player reachs the flag a new object appears with the massage that the lv is won. Then It shall open a new world after space key s pressed
danpost danpost

2012/11/19

#
Sl3nDeRm4n wrote...
And i have actors that move programmically around but its no problem if they stop
The thing is, they will not stop while waiting for the keypress; unless you programatically halt them while waiting for the keypress. You could add a static boolean to the world class, called something like 'actorsHalted', set to 'false'. Then in all actor classes whose movements are programmed, add the following statement as the first statement in the 'act' method:
if (WorldName.actorsHalted) return;
Set 'actorsHalted' to 'true' when you set 'waitingForSpaceKey' to 'true'. The boolean 'waitingForSpaceKey' should be declared in the class (not within a method). The static boolean 'actorsHalted' should be declared in the world class (again, not within a method).
Sl3nDeRm4n Sl3nDeRm4n

2012/11/19

#
it can move while waiting thats no problem but i do not check the intersecting in the flag class. I check it in my player class
danpost danpost

2012/11/19

#
The player class would be the best place to put the code.
Sl3nDeRm4n Sl3nDeRm4n

2012/11/19

#
ok ill try it thank you
Sl3nDeRm4n Sl3nDeRm4n

2012/11/19

#
now it does not open directly and not if i press space could that be because in this class i have no act() method? Im working with a method Action which gets parameter from a subclass?
Sl3nDeRm4n Sl3nDeRm4n

2012/11/19

#
ok i fised it now: Greenfoot doesnt know space it might have another name if i write
if(isKeyDown("n"){

}
and not
if(isKeyDown("space"){

}
it works
There are more replies on the next page.
1
2
3