Hello,
I am having a strange problem which I don't seem to be able to get around.
Say I have this:
This one works.
But this...
The problem is that sometimes, when I compile or reset the game/program, nothing happens when I press left or right (after clicking "ready"), unless I pause the game and resume, or I alt-tab to another window, and back. Other times, it works normally.
And it works correctly 100% of the time, when I check with Greenfoot.isKeyDown() (commented part of the above code).
It's not a question for a solution (since the solution for this is obviously to use the other way, which works, rather I need to know why this happens, so I avoid it happening in another, more complex code, that uses this same method, with the same symptoms.
1 2 3 4 5 6 | private String text = "" ; public void act(){ String input = Greenfoot.getKey(); if (input != null ) text += input; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public void act(){ String input = Greenfoot.getKey(); if (initialized && !ready){ if (Greenfoot.mouseClicked(trueButton)){ addSecondImage(); ready = true ; } } if (ready){ //if(Greenfoot.isKeyDown("left")){ if (input != null && input.equals( "left" )){ //while(Greenfoot.isKeyDown("left")){} userAnswer = true ; getNextImage(); } //if(Greenfoot.isKeyDown("right")){ if (input != null && input.equals( "right" )){ //while(Greenfoot.isKeyDown("right")){} userAnswer = false ; getNextImage(); } } } |