2013-08-19 157 views
2

我正在嘗試編寫此代碼,以便在點擊開始時從"FORM1"開始Obj將調用此方法來使用並啓用timer1Messagebox不會停止顯示

當我點擊開始按鈕犬的圖像將啓動但消息框一直顯示和dogpic到達球門線後不停止移動到右側,直到達到X= 620那麼它就會顯示消息框" win"

class dog 
{ 
    public int startpost; 
    public int TrackLenght = 620; 
    public PictureBox dogpic = null; 
    public int Location = 0; 
    public Random random=new Random(); 

    public void ResetStart() 
    { 
     dogpic.Location = new System.Drawing.Point(40, startpost); 
    } 

    public bool testrun() 
    { 
     Point p = dogpic.Location; 

     if (p.X < TrackLenght) 
     { 
      int distance = random.Next(5); 

      p.X = p.X + distance; 
      dogpic.Location = p; 
      Location = dogpic.Location.X; 
      return false; 
     } 
     else 
     { 
      MessageBox.Show(dogpic.Name + " win"); 

      return true; 
     } 
    } 
} 
+2

你的'計時器'在哪裏?停止它。 –

+0

我的timer_Tick是在FORM1我離開 timer.Enabled = true; – eathapeking

+0

你需要添加'timer_tick'的代碼,定時器有一個叫做Stop()的方法我相信 – Sayse

回答

3
//suppose dog1 is an instance of your dog class 
//here is the Tick event handler of your timer1 
private void timer1_Tick(object sender, EventArgs e){ 
    timer1.Enable = !dog1.testrun(); 
} 
+0

它不停止T^T ..... – eathapeking

+0

我應該在哪裏放messagebox.show – eathapeking

1

您可以使用計時器。

timer.Interval=5000; 
timer.Enabled=true; 
MessageBox.Show(dogpic.Name + " win"); 

您可以將其與tck事件相關聯。

private void timer_Tick(object sender,EventArgs evt) { 
    timer.Enabled=false; 
} 
+0

爲什麼downvote ??? –

+0

誰downvote - = - 我點擊upvote,但它-1> .... < – eathapeking

+0

我沒有明白你的觀點。我的答案是否對你錯?我想它會起作用。我絕對做你應該downvote :( –

1

嘗試正在重置p.X你贏了。

沒有看到它你的代碼,但我認爲你應該做這樣的事情:

public bool testrun() 
    { 

     Point p = dogpic.Location; 

     if (p.X < TrackLenght) 
     { 
      int distance = random.Next(5); 

      p.X = p.X + distance; 
      dogpic.Location = p; 
      Location = dogpic.Location.X; 
      return false; 
     } 
     else 
     { 

      MessageBox.Show(dogpic.Name + " win"); 
      ResetStart() 
      return true; 
     }}} 
1

在按一下按鈕,你應該調用ResetStart()函數,這將使定時器,做你的工作,並在到達終點它應該禁用定時器。

class dog 
{ 
    public int startpost; 
    public int TrackLenght = 620; 
    public PictureBox dogpic = null; 
    public int Location = 0; 
    public Random random=new Random(); 

    public void ResetStart() 
    { 
     dogpic.Location = new System.Drawing.Point(40, startpost); 
     timer.Enabled=true; 
    } 

    public bool testrun() 
    { 
     Point p = dogpic.Location; 

     if (p.X < TrackLenght) 
     { 
      int distance = random.Next(5); 

      p.X = p.X + distance; 
      dogpic.Location = p; 
      Location = dogpic.Location.X; 
      return false; 
     } 
     else 
     { 
      MessageBox.Show(dogpic.Name + " win"); 
      timer.Enabled=false;  
      return true; 
     } 
    } 
} 

希望它能工作。

+0

我應該在哪裏放messagebox.show – eathapeking