Author Topic: Pressing keys and Dialogues  (Read 1348 times)

himmanos

  • Member
  • **
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Pressing keys and Dialogues
« on: Fri, Apr 17, 2015 »
This problem is not just for dialogues, but I kind of noticed it there. I'm guessing it has something to do with the high refresh rate or something, but here it goes.

I've done the following:
Code: [Select]
if (FlxG.keys.justReleased("X") && !player._indialogue)
{
stopPlayer();
player._indialogue = true;
showDialog(20, 30, 1);
}

if (FlxG.keys.justReleased("X") && player._indialogue)
{
    dialog.kill();
}

Now, the first part works perfectly, as it creates a dialogue window.
The second part also does it's job, since it destroys the window, nevermind the missing adjustments for _indialogue variable and stuff.

The problem occurs when I press X. It basically does both of the things at once (or in succession). First it creates the window and at the same moment destroys it. I'm assuming that a couple of frames have gone by while I was pressing X, so both of the commands executed.

What I need is how to stop this from happening. Or rather, how do I force that you absolutely have to press the X button twice in order to destroy the dialog?
« Last Edit: Fri, Apr 17, 2015 by himmanos »

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: Pressing keys and Dialogues
« Reply #1 on: Fri, Apr 17, 2015 »
Nothing prevents the logic in both if-statements to be executed in the same frame - after the dialog was created in the first one, X was still "just released" in the second if-condition, and player._indialogue has als just been set to true.

You could restructure your code like this to prevent this:

Code: [Select]
if (FlxG.keys.justReleased("X"))
{
if (player._indialogue)
{
dialog.kill();
}
else
{
stopPlayer();
player._indialogue = true;
showDialog(20, 30, 1);
}
}