如何修复此无限循环/ 堆栈溢出错误

本文关键字:堆栈 栈溢出 错误 无限循环 何修复 | 更新日期: 2023-09-27 18:34:23

>我需要从 Form1 获取一个方法,但是当我调用它时,我得到一个无限循环错误。我得到这个是因为我在Form1开始时创建了一个新的GameManager类,并且我在GameManager中创建了一个新的Form1。如何在不出现此无限循环错误的情况下将方法从 form1 获取到游戏管理器中?

表格1:

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();
        }
    }
}

如何修复此无限循环/ 堆栈溢出错误

表单对象作为参数传递给 GameManager 对象。 例如,在游戏管理器的构造函数中,创建另一个参数"Form1 form"并执行 myForm = form;

从 Form1 调用构造函数时,将"this"作为参数传递。

此外,

如果您不想传递对整个表单的引用,则可以只将委托传递给构造函数GameManager。有关参会代表的更多信息,请点击此处

将构造函数添加到GameManager

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

然后Form1

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