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

2012/5/10

GUI Freezes/Locks Up With Keyboard Use?

londovir londovir

2012/5/10

#
I've been writing a series of classic arcade games in Greenfoot (including "Breakout" and "Space Invaders") for my students to practice on in my AP course at the high school I teach at. To do so, I've been working on a variety of computers (Windows XP and Windows 7, primarily), utilizing originally Greenfoot 2.1.2 with Java 7 (Update 4) JDK. Recently I upgraded to Greenfoot 2.2.0. I'm coming across an odd "bug" in my work which I cannot determine the source of. In all games where I use the keyboard to control the movement of an object on the screen (paddle in Breakout, turret in Space Invaders), I typically put keyboard detection code in the act() method of the moving object itself. I've created a basic structure to use the Greenfoot.isKeyDown() method to determine if the left arrow or right arrow is being pressed, and if so, to move the actor appropriately on the screen. If I run this under the Greenfoot IDE (and, as it turns out, even in a compiled JAR file version), the game will periodically freeze up at random times, for random amounts of time. In Breakout the ball will cease motion, and it will appear that the game has stopped. In Space Invaders, every actor stops dead and quits moving. It's as though the act() methods of all actors quits being called (perhaps the World/Greenfoot stops sending act() calls to the objects?). However, in addition the GUI itself becomes nearly unresponsive; clicking on the "Pause" button will not work. If I view Windows Task Manager, the java.exe process reports nearly 0% process use. During these random "freezes", I've learned that I can press ALT+S to toggle open the "Scenario" menu at the top of the screen, and immediately press ALT to dismiss that menu, and this will restore operation to the active World process. This happens without fail when the freezes occur. If, instead, I click the "Reset" button, the GUI world space is cleared, but will not reset properly on its own. For instance, my current computer resolution is smaller than the size of the world I'm creating, so there are scrollbars to help scroll the game world. When this occurs, the world clears to a white rectangle of space and the scrollbars don't properly repaint, so it's as though the standard Windows/Java message queue process is being frozen. I can open the debugger panel and terminate the process, at which point again the GUI becomes responsive. To test a theory, I edited my code to allow the Breakout ball to rebound off all 4 walls of the screen, ran the scenario at full speed on the slider bar, and sat back. It never once froze, and at times pegged my process at 25% CPU. This leads me to believe there's something going on with the Java subsystem that is perhaps overloading the system due to the arrow keys being held down. I've noticed if I use the Greenfoot.getKey() method to "consume" keypresses as part of the isKeyDown handler code, that freeze ups occur less frequently, but they still occur. In most cases, when a longer freeze occurs, I can move the speed slider to maximum, not press an arrow key, and eventually the game will spontaneously continue on its own. Again, I'm not sure if this is somehow a case of thousands of "keyDown" messages are getting piled into a buffer, bogging down the Java/Windows system and by not pressing any keys and running the GUI at full speed will eventually clear those out. Has anyone else experienced this behavior? There are no while, do-while, or for structures in my code, so I'm doubting that there's anything causing an inadvertent infinite loop - and besides, I find it interesting that I can immediately get the GUI to become responsive and active by toggling a menu on the menu bar. As a programmer, that makes me think it's somehow related to the messaging queue or Windows in general, Java subsystem perhaps as well. I've only so far worked with the Java 7u4 JDK at this point, and I know Java 7 has had some significant issues. I wasn't sure if I could run Greenfoot 2.2 under a more stable Java 6 environment, but I'm beginning to consider rolling back all of my systems to Java 6. This constant, random freezing is making Greenfoot almost impossible for me to use in my school since it interferes with the design of full-motion games. Thanks for any info anyone has about this. I'm curious to know if it's only me, or if there's an issue I don't know about. Londovir
SPower SPower

2012/5/10

#
Do you use a while loop together with Greenfoot.isKeyDown()?
davmac davmac

2012/5/10

#
londovir, this is certainly not an issue we (the development team) have noticed. If you like you could send a problematic scenario to support@greenfoot.org and someone will see if they can reproduce the problem or figure out what's causing it. It does sound quite unusual though, and I'm thinking most likely it's an obscure Java bug.
londovir londovir

2012/5/10

#
Success! I believe I have narrowed down at least the common factor in what's happening here through a series of differentiated environments. It's the scrollable World pane that appears to be the culprit for me. My world is set at a resolution of 616x672 (1 pixel resolution). Therefore, on some computers that I use in my work office (which have flat panel monitors driving a 1280x1024 screen), the entire World pane window appears intact on the screen. If I run my games under these contexts, I do not see the freezing GUI behavior I described at all. However, if I run the exact same code when the monitor is set to a lower resolution (such as 1024x768) which causes the World pane to be scrollable via scrollbars, the game will randomly and sporadically freeze up, generally at the moment when the arrow keys I'm using to move the paddle also move the World pane, or quite soon after. In fact, if I take the lower resolution work environment and modify my isKeyDown handler code to inspect any other keyboard key, such as perhaps a typical AWSD arrangement of keys, rather than the standard left arrow "left" and right arrow "right", this behavior disappears completely. No freeze issues at all. My assumption must be that when the window is too small on the screen to display the World pane in its entirety (and therefore scrollbars appear), using isKeyDown with "left" and "right" (for arrow keys) causes a conflict when, at times, the keys are being used both for movement of an actor along with movement of the World pane itself. Perhaps that's why I can use ALT+S to open the "Scenario" drop-down menu, and then use ALT alone to dismiss the menu, and the game will immediately continue onward....it could be redirecting message queue processing back to the Greenfoot engine, when perhaps it's getting lost in the Java messaging subsystem? I can send a baseline code project to support if you'd like to see it for yourself, but I have to believe I've found the issue. I'll have to assume the problem lies with Java and not within Greenfoot, as no one else has noticed this before me, but at this point I've tested 1 version of my code against GF 2.1.1, GF 2.2.0, Java 6u32, Java 7u4, on Win XP, Win Vista, Win 7, 1024x768, 1280x1024, and oddly enough the only variable that mattered was the resolution, so I'm thinking I figured it out. For now, I have to avoid using the "left" and "right" keys if the World pane is too large to fit on the screen. Thanks! Londovir
DogPappy DogPappy

2012/9/28

#
I too have found this same issue. Thank you, thank you, for posting your findings! I was getting close to identifying the scrollbars as the culprit as well when I found your post. Too bad I didn't find it earlier, I would of saved some hair. For now, I think I'll use the A,W,S,D key's for direction.
mjrb4 mjrb4

2012/9/28

#
The scrollbars do indeed seem like the culprit, and yes by default because the arrow keys also scroll the contents of the scroll pane as well as control the actor, this was causing issues. As for the technical reason for the "lockups" (though they were brief and not that disruptive at all on my machine), I've no idea - it could be all number of things. I'm not sure that's hugely relevant though, it seems here though the root of the problem is we don't want the arrow keys scrolling the world at all. I've disabled that behaviour now, so in the next release this issue should be fixed.
You need to login to post a reply.