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

2025/1/6

Greenfoot.ask() window doesn't close on web

sthompson sthompson

2025/1/6

#
I'm uploading a project to greenfoot.org that uses Greenfoot.ask() to get an input from the player. It works fine in the greenfoot application, but now that I've uploaded it, the window prompting the player never closes. Is there any workaround for this? You can see it in action here (use the arrow keys to walk to the tree monster, and then follow the prompts): https://www.greenfoot.org/scenarios/34529
danpost danpost

2025/1/7

#
sthompson wrote...
I'm uploading a project to greenfoot.org that uses Greenfoot.ask() to get an input from the player. It works fine in the greenfoot application, but now that I've uploaded it, the window prompting the player never closes. Is there any workaround for this? You can see it in action here (use the arrow keys to walk to the tree monster, and then follow the prompts): https://www.greenfoot.org/scenarios/34529
Looks to me like it is working fine. I think your code is making it repeatedly perform the "ask" action. For assistance, either show your codes here or re-upload the scenario having the "Publish source" box checked this time.
sthompson sthompson

2025/1/20

#
Hmm, the code only performs "ask" once as far as I can tell. I reuploaded the code with "publish source" checked: https://www.greenfoot.org/scenarios/34529 Here's a video of it in the browser vs. locally so you can see the difference: https://youtu.be/E62BC6Rj6ps
danpost danpost

2025/1/20

#
sthompson wrote...
Hmm, the code only performs "ask" once as far as I can tell. I reuploaded the code with "publish source" checked: << Links Omitted >>
I noticed two things in your Textbox class. The first is in the act method where you check for input. The check however does not limit the input to a specific response. The programming there is considered "loose", where any correct input at the wrong time would trigger the wrong action to proceed with. The other thing was how you are comparing strings. Always make sure that the "noun" of the expression is not null. That is:
// instead of (for example)
if (answer.equals("30"))

// use
if ("30".equals(answer))
Absolutely no problems with the literal as the "noun" of the expression. If both have the possibility of being null, you would have to check one for null and then use it to compare (after finding out it is NOT null).
sthompson sthompson

2025/1/21

#
Thanks! It's a student project, and there are definitely questionable coding practices—I'm just trying to get the web version running well enough that they can share it. Changing the act() code to call getKey() once and then use that key to decide what to do fixed the issue, though (I think using isKeyPressed() in act() was resulting in multiple text boxes opening on top of each other in web, whereas in Greenfoot the textbox opening pauses the program. Regardless, it's working well enough now. Thanks for taking a look!
danpost danpost

2025/1/22

#
sthompson wrote...
Changing the act() code to call getKey() once and then use that key to decide what to do fixed the issue, though (I think using isKeyPressed() in act() was resulting in multiple text boxes opening on top of each other in web, whereas in Greenfoot the textbox opening pauses the program.
Still each string comparison should be accompanied with a check to ensure that the string returned is for the question asked. That is
// in pseudo-code
if "responding to first textbox AND answer is 'Y'", {  ... }
if "responding to second textbox AND answer is "1" { ... }
// etc.
In fact, I think
You need to login to post a reply.