2013-10-07 78 views
0

我需要從Form1中獲取一個方法,但是當我調用它時,會出現無限循環錯誤。我得到這個是因爲我在Form1的開頭創建了一個新的GameManager類,並且我在GameManager中創建了一個新的Form1。我怎樣才能得到一個方法從form1到GameManager沒有得到這個無限循環錯誤?我該如何解決這個無限循環/ StackOverFlow錯誤?

Form1中:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace CatAndMouse 
{ 
    public partial class Form1 : Form 
    { 
     GameManager myGM = new GameManager(); 
     int dir = 0; 
     public Form1() 
     { 
      InitializeComponent(); 
      newGame(); 
     } 

     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      if (this.myGM != null) 
       this.myGM.paint(e.Graphics); 
      //e.Graphics.DrawImage(imgMouse.Images[0], pointXMouse, pointYMouse); 
      //e.Graphics.DrawImage(imgCat.Images[0], 50, 100); 
      //e.Graphics.DrawImage(imgCheese.Images[0], 75, 100); 
     } 

     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Up) 
      { 
       dir = 0; 
      } 
      if (e.KeyCode == Keys.Right) 
      { 
       dir = 1; 
      } 
      if (e.KeyCode == Keys.Down) 
      { 
       dir = 2; 
      } 
      if (e.KeyCode == Keys.Left) 
      { 
       dir = 3; 
      } 
     } 
     public void newGame() 
     { 
      timer1.Start(); 
      myGM.newGame(imgCat, imgMouse, imgCheese); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      pictureBox1.Refresh(); 
     } 
     public int getDir() 
     { 
      return dir; 
     } 
    } 
} 

遊戲管理:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace CatAndMouse 
{ 
    class GameManager 
    { 
     Form1 myForm = new Form1(); 
     Cat ca1 = new Cat(); 
     Mouse m = new Mouse(); 
     Cheese ch = new Cheese(); 
     int amount = 5; 
     int catdir = 0; 
     Timer time = new Timer(); 
     public ImageList imgCat = new ImageList(); 
     public ImageList imgMouse = new ImageList(); 
     public ImageList imgCheese = new ImageList(); 

     public void newGame(ImageList cat, ImageList mouse, ImageList cheese) 
     { 
      imgCat = cat; 
      imgMouse = mouse; 
      imgCheese = cheese; 
      time.Start(); 
     } 

     public void move() 
     { 
      ca1.Move(amount); 
      m.Move(amount); 
     } 

     public void paint(Graphics g) 
     { 
      g.DrawImage(imgCat.Images[0], ca1.getLocation()); 
     } 

     private void time_Tick(object sender, EventArgs e) 
     { 
      move(); 
      getDir(); 
     } 
     public void getDir() 
     { 
      catdir = myForm.getDir(); 
     } 
    } 
} 
+0

你想訪問什麼方法? – Brian

回答

2

傳遞表單對象作爲參數傳遞給您的遊戲管理對象。例如,在你的GameManager的構造函數中做另一個爭論'Form1窗體'並且執行myForm = form;

從Form1調用構造函數時,將'this'作爲參數傳遞。

+0

非常感謝!這工作得很好! –

0

如果您不想傳遞整個表單的引用,那麼您可以僅將一個代理傳遞給GameManager構造函數。有關委託here

0

更多信息添加一個構造函數來GameManager

Form1 myForm; 
public GameManager(Form1 form) 
{ 
    myForm = form; 
} 

然後Form1

GameManager myGM; 
public Form1() 
{ 
    myGM = new GameManager(this); 
}