2013-04-26 77 views
0

我試圖做一個單一的遊戲,但我在這裏得到了一個小問題... 我正在移動帶箭頭鍵的圖片盒,以避免其他圖片盒...問題是,我的圖片盒移出當我按下左鍵太多次時,我就成功地解決了這個問題(通過阻塞另一個盒子),但左側版本仍然不起作用,知道爲什麼...如何在移動時將圖片框保持在窗體中?

下面是代碼:

if (pictureBox7.Bounds.IntersectsWith(pictureBox1.Bounds)) 
      switch (e.KeyCode) 
      { 
       case Keys.Escape: Application.Exit(); break; 
       case Keys.P: timerkunai1.Enabled = false; 
        timerkunai2.Enabled = false; timerkunai3.Enabled = false; 
        timerkunai4.Enabled = false; timerninja.Enabled = false; 
        timerlife.Enabled = false; 
        button3.Show(); break; 
       case Keys.Right: i = 6; dx = 25; press = true; break;    
      } 
     if (pictureBox8.Bounds.IntersectsWith(pictureBox1.Bounds)) 
      switch (e.KeyCode) 
      { 
       case Keys.Escape: Application.Exit(); break; 
       case Keys.P: timerkunai1.Enabled = false; 
        timerkunai2.Enabled = false; timerkunai3.Enabled = false; 
        timerkunai4.Enabled = false; timerninja.Enabled = false; 
        timerlife.Enabled = false; 
        button3.Show(); break; 
       case Keys.Left: i = 0; dx = -25; press = true; break; 
      } 
     else 
      switch (e.KeyCode) 
     { 
      case Keys.Escape: Application.Exit(); break; 
      case Keys.P: timerkunai1.Enabled = false; 
       timerkunai2.Enabled = false; timerkunai3.Enabled = false; 
       timerkunai4.Enabled = false; timerninja.Enabled = false; 
       timerlife.Enabled = false; 
       button3.Show(); break; 
      case Keys.Left: i = 0; dx = -25; press = true; break; 
      case Keys.Right: i = 6; dx = 25; press = true; break; 
     } 

回答

0

需要編寫代碼,以檢查是否在PictureBox的邊界是形式之外。如果圖片框移動將導致它超出邊界,則阻止移動。 類似這樣的僞代碼: if (pictureBoxZ + dx < 0 || pictureBoxZ + dx > pictureBoxZ.Parent.Width) { //Deny Motion }

0

您的dx變量是否爲PictureBox新位置的偏移量? 然後限制Location.x以0:

if (pictureBox1.Location.x + dx > 0) 
    pictureBox1.Location += dx; 

如果你想限制左右大小,窗體使用的寬度,這樣的代碼:

if ((pictureBox1.Location.x + dx > 0) && (pictureBox1.Location.x + dx < this.Size.Width - pictureBox1.Size.Width)) 
    pictureBox1.Location += dx; 
相關問題