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;
}
}
}
GameManager :
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();
}
}
}
GameManager
에 생성자를 추가 하시겠습니까? – Brian