0
키보드 키로 그래픽을 움직이기 원하는 프로그램이 있습니다. 원래 버튼을 누를 때마다 그래픽을 이동하는 기능이 있었지만 키보드 반복 지연을 피하기 위해 그 방법을 포기했습니다. 대신 KeyPess 이벤트를 활성화하고 KeyUp 이벤트를 비활성화하는 타이머를 사용하기로 결정했습니다. 처음에는 각기 다른 방향에 대해 4 가지 타이머를 사용했는데 효과가 있었지만 프로그램이 꽤 자주 멈추기 시작했음을 알았습니다. 모든 움직임에 대해 하나의 타이머를 사용하고 if 문을 사용하여 방향을 결정하기로했습니다. 이제는 그래픽이 전혀 움직이지 않는 것처럼 보입니다. 비록 내가 한 일은 복사하여 붙여 넣기 코드였습니다.왜 내 물건이 움직이지 않습니까?
enum Direction
{
Left, Right, Up, Down
}
private Direction _objectDirection;
int _x = 100, _y = 100;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Picture.MakeTransparent(Color.Transparent);
e.Graphics.DrawImage(Picture, _x, _y);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
if (timerAnimation.Enabled == false)
{
AnimationMaxFrame = 3;
timerAnimation.Enabled = true;
}
_objectDirection = Direction.Up;
timerMovement.Enabled = true;
}
//The rest of this code is omitted to save space, it is repeated 4 times with the only
//changes being the key pressed, and the object direction.
Invalidate();
}
void Form1_KeyUp(object sender, KeyEventArgs e)
{
timerAnimation.Enabled = false;
timerMovement.Enabled = false;
Picture = Idle;
this.Refresh();
}
private void timerMovement_Tick(object sender, EventArgs e)
{
if (_objectDirection == Direction.Up)
{
if (_y > 24)
{ _y = _y - 2; }
else
{ timerMovement.Enabled = false; }
//This if statement is to ensure that the object doesn't leave the form.
//I have tried removing them already, they're not the problem.
}
//Again, this is shortened to save space, it is repeated for each direction.
Invalidate();
}
왜 내 그래픽이 움직이지 못하게합니까? 더 좋은 방법이 있습니까? 아직이 기능에 추가하고 싶은 기능이 많이 있지만 이미 멈췄습니다.
가 왜 2 개 개의 타이머가 있습니까 :
그것은이 (예제 코드) 같은 것입니까? –
타이머 중 하나는 애니메이션 용이고 다른 하나는 이동 용입니다. – user3303233