2017-05-07 35 views
0

好吧,所以我的問題如下,我有一個計時器,每隔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 
     } 
    } 
} 

}

+0

remove事件Timer1_Tick成品或只加一次 –

+0

你什麼意思通過添加只有1次 – Jamisco

+1

每次處理此命令時:timer.Tick + = Timer1_Tick;,再次添加事件,導致滴答執行次數與事件添加次數相同。 –

回答

1

不要設置按鈕按下計時器 - 有一個計時器每2秒,如果按下並採取行動,如果它是按鈕,檢查滴答作響。 如(僞):

public Form1() 
{ 
    timer.Enabled = true; 
    timer.Interval = 2000; 
} 

KeyCode keyPressed; 

private void Form1_KeyDown(object sender, KeyEventArgs e) 
{ 
    keyPressed = e.KeyCode; 
} 

private void Timer1_Tick(object sender, EventArgs e) 
{ 
    if (keyPressed == Keys.D) 
    { 
     // do something 
    } 
} 

更新來解決新代碼: 如果你想取消設置「canMoveR」你可能要設置一個超時變量 - 是這樣的:

int timeout = 0; 
private void Form1_KeyDown(object sender, KeyEventArgs e) 
{ 
    keyPressed = e.KeyCode; 
    timeout = 2; // 2 seconds assuming your timer ticks every 1000ms 
} 

然後在計時器滴答:

private void Timer1_Tick(object sender, EventArgs e) 
{ 
    --timeout; //will go down to 1 the first tick, then down to 0 on the second 
    if (timeout == 0) 
    { 
     canMoveR = false; 
    } 
} 
+0

所以我在Timer1_tick事件中創建了if語句,它檢測bool變量「isMovingR」是否設置爲true,當我按下D時,「isMovingR」變量將被設置爲true。現在,當ismovingR設置爲true時,timer_tick事件if語句檢測到使變量「canMoveR」等於true,然後在OnPaint中有一個if語句檢測「canMoveR」變量是否爲真,是否它相應地繪製了框架,但是如何在2秒後禁用「canMoveR」,我將構造並更新代碼 – Jamisco

+0

請參閱編輯以禁用canMoveR – Kirlac

0

每次按d,加入Timer1_Tick的情況下,所以,在第二次按下d,事件運行2次。在構造函數上設置事件。

public Form1() 
    { 
     InitializeComponent(); 
      timer.Enabled = false; 
      timer.Interval = 2000;     
      timer.Tick += Timer1_Tick; 

    } 


    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; 
      //imageX += 5; 
    timer.Enabled = true; 
      Refresh(); 
     } else if (e.KeyCode == moveLeft) 
     { 
      isMovingL = true; 
      imageX -= 5; 
      Refresh(); 
     } 

    } 
+0

謝謝,但你的解決方案不起作用,也許你在某個地方犯了一個錯誤? – Jamisco

+0

好的,我誤解了你的問題。抱歉。你可以像kirlac說的那樣做。 –

+0

不用擔心m8,你仍然有幫助,我現在知道如何解決我的問題的另外一種方法 – Jamisco