好吧,所以我的問題如下,我有一個計時器,每隔2秒(測試目的)畫一個幀,當我按D(按下我的意思是D鍵不斷按下),無論如何,當我第一次按下D時,表單將等待2秒鐘,然後根據需要繪製圖像,但問題是它只能這樣做一次。因此,在2秒後,剩餘幀將以表單可以繪製的最快速度繪製。所以,我怎麼確保幀將被連續放出只有每2秒,同時保持2秒的間隔不管我有多長按d鍵...這裏是我的代碼如何在一個連續的循環中保持定時器(winforms定時器)
public partial class Form1 : Form
{
Keys moveRight;
Keys moveLeft;
public static bool isMovingR = false;
public static bool isMovingL = false;
public static bool canMoveR = false;
Bitmap stnd = new Bitmap(Properties.Resources.Standing);
static Bitmap wlk_1_RL = new Bitmap(Properties.Resources.Walk_1_RL);
static Bitmap wlk_3_RL = new Bitmap(Properties.Resources.Walk_3_RL);
static Bitmap wlk_4_LL = new Bitmap(Properties.Resources.Walk_4_LL);
static Bitmap wlk_6_LL = new Bitmap(Properties.Resources.Walk_6_LL);
Animation animate = new Animation(new Bitmap[] { wlk_1_RL, wlk_3_RL,
wlk_4_LL, wlk_6_LL });
Timer timer = new Timer();
int imageX = 5;
int imageY = 234;
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
if (isMovingR == false)
{
e.Graphics.DrawImage(stnd, imageX, imageY);
//Refresh();
}
if (canMoveR == true)
{
e.Graphics.DrawImage(animate.Frame2Draw(), imageX, imageY);
timer.Stop();// this simply stops the animation from continuing once I have stopped press D
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
moveRight = Keys.D;
moveLeft = Keys.A;
if (e.KeyCode == moveRight)
{
isMovingR = true;
timer.Enabled = true;
timer.Interval = 2000;
timer.Tick += Timer1_Tick;
//imageX += 5;
Refresh();
} else if (e.KeyCode == moveLeft)
{
isMovingL = true;
imageX -= 5;
Refresh();
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D)
{
isMovingR = false;
canMoveR = false;
Animation.slctdImg = 0;
this.Refresh();
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
canMoveR = true;
this.Invalidate(); // calls the OnPaint event
timer.Stop(); // testing purposes and also to let you know timer.stop() doesn't really fix my problem in this case
}
}
}
更新的代碼,基於Kirak溶液
public Form1()
{
InitializeComponent();
timer.Enabled = true;
timer.Interval = 1000;
timer.Tick += Timer1_Tick;
}
protected override void OnPaint(PaintEventArgs e)
{
if (canMoveR == true)
{
e.Graphics.DrawImage(animate.Frame2Draw(), imageX, imageY);
timer.Stop();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
moveRight = Keys.D;
moveLeft = Keys.A;
if (e.KeyCode == moveRight)
{
isMovingR = true;
//imageX += 5;
Refresh();
} else if (e.KeyCode == moveLeft)
{
isMovingL = true;
imageX -= 5;
Refresh();
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D)
{
isMovingR = false;
canMoveR = false;
Animation.slctdImg = 0;
this.Refresh();
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
if (isMovingR == true)
{
canMoveR = true;
this.Invalidate(); // calls the OnPaint event, hence all things that the OnPaint
//event draws, will be redrawn, you make ask your self why i would need
//the canMoveR to be set to true then, this is because i plan on adding more things
// so for future purpose, all things to be drawn in the game will
// will only be drawn when i want them to be drawn or under specific circumstances
}
}
}
}
remove事件Timer1_Tick成品或只加一次 –
你什麼意思通過添加只有1次 – Jamisco
每次處理此命令時:timer.Tick + = Timer1_Tick;,再次添加事件,導致滴答執行次數與事件添加次數相同。 –