1

I've got a problem with a keypress event. I have a function that returns true or false on a keypress like this: (simplified a bit)

onkeydown (keycode == 88) xKey = true;
onkeyup    (keycode == 88) xKey = false;

if (xKey == true && bombs > 0)

    {
          this.bomb;
          bomb -= 1;
    }

Now, when you press the key, it decrements the variable to long, minimal 2 counts which doesn't add to the gameplay of course, I'm not using jQuery which has an unbind attribute and I don't want to add jQuery either, what's the old-school way? I cant find any relevant information on this but I guess I'm not the first to encounter this problem.

4

2 に答える 2

1

onKeyPress対 onKeyUpおよびonKeyDown

onkeypressは両方で、keydownの後にkeyupが続きます。

したがって、onkeypressを実行すると、何があってもonkeypressコードとonkeyupコードが実行されます。

代わりにonkeydownを使用して追跡し、onkeyup機能に干渉しないようにしたい場合があります。

于 2012-09-11T19:12:40.347 に答える
0

keypress は、キーが押されている限り繰り返し発生します。キーダウンまたはキーアップを使用するか、間隔またはタイムアウトを介してスロットルを拡張して、最後のキープレスイベントでのみリクエストをキューに入れることができます

于 2012-09-12T08:36:18.010 に答える