c# - Removing the delay after KeyDown event? -
when hold key in game move player:
public void mainform_keydown(object sender, keyeventargs e) { if (e.keycode == keys.up) { player.moveup(); } }
the player instantly moves 1 step press down arrow, , pauses short duration before starting smoothly move again. why's this? how can prevent it?
the answer in the proposed duplicate incorrect, unfortunately. doesn't ignore repeated keydown
events, , gradually increase "delta" value in direction being handled each key case. doesn't respond keypress (i.e. no action happens until first timer tick).
this answer holding arrow keys down character movement c# .net issues explains how ignore subsequent keydown
events, doesn't explain how character move.
in other words, couldn't find duplicate question correctly answers question. so…
the basic technique want is:
- don't move on actual key input. instead, generate own timing logic move object.
- instead of using
keydown
event move object, use set movement direction, processed timing logic.
there variety of ways accomplish this. 1 version this:
private bool _moveup; private bool _movedown; private bool _moveleft; private bool _moveright; // can add timer in winforms designer instead if like; // interval property can configured there @ same time, along // tick event handler, simplifying non-designer code here. private system.windows.forms.timer _movementtimer = new timer { interval = 100 }; public mainform() { initializecomponent(); _movementtimer.tick += movementtimer_tick; } private void movementtimer_tick(object sender, eventargs e) { _domovement(); } private void _domovement() { if (_moveleft) player.moveleft(); if (_moveright) player.moveright(); if (_moveup) player.moveup(); if (_movedown) player.movedown(); } // of course override onkeydown() method instead, // assuming handler in form subclass generating // event. public void mainform_keydown(object sender, keyeventargs e) { if (e.isrepeat) { // ignore key repeats...let timer handle return; } switch (e.keycode) { case keys.up: _moveup = true; break; case keys.down: _movedown = true; break; case keys.left: _moveleft = true; break; case keys.right: _moveright = true; break; } _domovement(); _movementtimer.start(); } public void mainform_keyup(object sender, keyeventargs e) { switch (e.keycode) { case keys.up: _moveup = false; break; case keys.down: _movedown = false; break; case keys.left: _moveleft = false; break; case keys.right: _moveright = false; break; } if (!(_moveup || _movedown || _moveleft || _moveright)) { _movementtimer.stop(); } }
do note timer objects in .net have limited resolution. show interval of 100 ms (10 times per second) above (same in other question's answer), , frequent update you're going reliably get. then, timer's tick
event may not (and won't) raised on exactly 100 ms intervals. there variation , forth. close enough basic game.
if need more precision that, have implement own state-polling-and-animation loop somewhere. that's whole other ball o' wax. :)
Comments
Post a Comment