Delphi7,按向上键进行形状跳转 [英] Delphi7, make a shape jump when pressing Up key
问题描述
当玩家按UP键时,我想做一个形状的跳跃,所以我可以想到的是最好的,但是我使用的方法是可怕的和有问题的:
I'd like to make a shape jump when the player presses the UP key, so the best i could think of is this, but the method i used is terrible and problematic:
(形状坐标:shape1.top:=432;)
(shape coordinates: shape1.top:=432;)
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case key of
vk_up: shape1.top:=shape1.top-40 //so that it jumps to 392
end;
end;
现在这个计时器:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
timer1.interval:=300
if shape1.Top<400 then //if shape1.top=392 < 400
begin
shape1.Top:=432; //move back to 432
end;
end;
问题是玩家可以不断按键UP,我不想要。我知道这种方法是可怕的,所以我希望你有一些比这更好的东西,如果你能与我分享,我将不胜感激。
The problem is that players can constantly press the key UP, which I don't want. I know this method is terrible, so i hope you have something better than this and i would be grateful if you could share it with me.
推荐答案
如果玩家可以按住一个键并且KeyDown反复触发,你可以锁定它。
If the player can hold down a key and KeyDown fires repeatedly, you can lock it.
首先,在名为 FKeyLock的表单上声明一个字段:字节集
。 (注意:如果您获得高于255的任何 Key
值,但这种技术将失败,但您可能处理的值不会太高。)
First, declare a field on the form called FKeyLock: set of byte
. (Note: this technique will fail if you get any Key
values higher than 255, but the ones you're likely to deal with won't be that high.)
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key in FKeyLock then
Exit;
case key of
vk_up:
begin
shape1.top:=shape1.top-40; //so that it jumps to 392
include(FKeyLock, vk_up);
end;
end;
end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
exclude(FKeyLock, key);
end;
这篇关于Delphi7,按向上键进行形状跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!