【问题标题】:error CS0115: 'Pong.Form1.Dispose(bool)': no suitable method found to override错误 CS0115:“Pong.Form1.Dispose(bool)”:找不到合适的方法来覆盖
【发布时间】:2015-03-21 02:59:46
【问题描述】:

我试图编译这段代码,但它不起作用,编译时出现这个错误:

Pong\Form1.Designer.cs(14,33,14,40): 错误 CS0115: 'Pong.Form1.Dispose(bool)': 找不到合适的方法来覆盖

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace Pong
{
    public partial class gameArea : Form
    {
        PictureBox picBoxPlayer, picBoxAI, picBoxBall;
        Timer gameTime; // also the game loop

        const int SCREEN_WIDTH = 800;
        const int SCREEN_HEIGHT = 600;

        Size sizePlayer = new Size(25, 100);
        Size sizeAI = new Size(25, 100);
        Size sizeBall = new Size(20, 20);

        const int gameTimeInterval = 1;

        const int ballStartSpeed = 2;
        const int ballIncreaseSpeedRate = 1;
        const int ballSpeedLimited = 15;

        const int aiOffSetLoops = 15;

        int ballSpeedX = ballStartSpeed;
        int ballSpeedY = ballStartSpeed;


        Random rad;
        int aiOffSet;
        int aiOffSetCounter;

        Dictionary<string, SoundPlayer> sounds;

        public gameArea()
        {
            InitializeComponent();

            this.DoubleBuffered = true;

            picBoxPlayer = new PictureBox();
            picBoxAI = new PictureBox();
            picBoxBall = new PictureBox();

            gameTime = new Timer();
            gameTime.Interval = gameTimeInterval;

            gameTime.Tick += new EventHandler(gameTime_Tick);

            this.Width = SCREEN_WIDTH;
            this.Height = SCREEN_HEIGHT;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = Color.Black;

            picBoxPlayer.Size = sizePlayer;
            picBoxPlayer.Location = new Point(picBoxPlayer.Width / 2, ClientSize.Height / 2 - picBoxPlayer.Height / 2);
            picBoxPlayer.BackColor = Color.Blue;
            this.Controls.Add(picBoxPlayer);

            picBoxAI.Size = sizeAI;
            picBoxAI.Location = new Point(ClientSize.Width - (picBoxAI.Width + picBoxAI.Width / 2), ClientSize.Height / 2 - picBoxPlayer.Height / 2); // TODO: why picBoxPlayer and not picBoxAI?
            picBoxAI.BackColor = Color.Red;
            this.Controls.Add(picBoxAI);

            rad = new Random();
            aiOffSet = 0;
            aiOffSetCounter = 1;

            picBoxBall.Size = sizeBall;
            picBoxBall.Location = new Point(ClientSize.Width / 2 - picBoxBall.Width / 2, ClientSize.Height / 2 - picBoxBall.Height / 2);
            picBoxBall.BackColor = Color.Green;
            this.Controls.Add(picBoxBall);

            // Load Sounds
            sounds = new Dictionary<string, SoundPlayer>();
            for (int k = 1; k <= 10; k++)
            {
                sounds.Add(String.Format(@"pong{0}", k), new SoundPlayer(String.Format(@"pong{0}.wav", k)));
            }

            // Start Game loop
            gameTime.Enabled = true;
        }

        void gameTime_Tick(object sender, EventArgs e)
        {
            picBoxBall.Location = new Point(picBoxBall.Location.X + ballSpeedX, picBoxBall.Location.Y + ballSpeedY);
            gameAreaCollosions();
            padlleCollision();
            playerMovement();
            aiMovement();
        }

        private void iaChangeOffSet()
        {
            if (aiOffSetCounter >= aiOffSetLoops)
            {
                aiOffSet = rad.Next(1, picBoxAI.Height + picBoxBall.Height);
                aiOffSetCounter = 1;
            }
            else
            {
                aiOffSetCounter++;
            }
        }

        private void gameAreaCollosions()
        {
            if (picBoxBall.Location.Y > ClientSize.Height - picBoxBall.Height || picBoxBall.Location.Y < 0)
            {
                iaChangeOffSet();
                ballSpeedY = -ballSpeedY;
                sideCollision();
            }
            else if (picBoxBall.Location.X > ClientSize.Width)
            {
                padlleSideCollision();
                resetBall();
            }
            else if (picBoxBall.Location.X < 0)
            {
                padlleSideCollision();
                resetBall();
            }
        }
        private void resetBall()
        {
            if (ballSpeedX > 0)
                ballSpeedX = -ballStartSpeed;
            else
                ballSpeedX = ballStartSpeed;
            if (ballSpeedY > 0)
                ballSpeedY = -ballStartSpeed;
            else
                ballSpeedY = ballStartSpeed;

            aiOffSet = 0;
            picBoxBall.Location = new Point(ClientSize.Width / 2 - picBoxBall.Width / 2, ClientSize.Height / 2 - picBoxBall.Height / 2);
        }
        private void playerMovement()
        {
            if (this.PointToClient(MousePosition).Y >= picBoxPlayer.Height / 2 && this.PointToClient(MousePosition).Y <= ClientSize.Height - picBoxPlayer.Height / 2)
            {
                int playerX = picBoxPlayer.Width / 2;
                int playerY = this.PointToClient(MousePosition).Y - picBoxPlayer.Height / 2;

                picBoxPlayer.Location = new Point(playerX, playerY);
            }
        }
        private void aiMovement()
        {
            int aiX = ClientSize.Width - (picBoxAI.Width + picBoxAI.Width / 2);
            int aiY = (picBoxBall.Location.Y - picBoxAI.Height / 2) + aiOffSet;

            if (aiY < 0)
                aiY = 0;
            if (aiY > ClientSize.Height - picBoxAI.Height)
                aiY = ClientSize.Height - picBoxAI.Height;

            picBoxAI.Location = new Point(aiX, aiY);
        }
        private void padlleCollision()
        {
            if (picBoxBall.Bounds.IntersectsWith(picBoxAI.Bounds))
            {
                picBoxBall.Location = new Point(picBoxAI.Location.X - picBoxBall.Width, picBoxBall.Location.Y);
                ballSpeedX = -ballSpeedX;
                aiCollision();
            }
            if (picBoxBall.Bounds.IntersectsWith(picBoxPlayer.Bounds))
            {
                picBoxBall.Location = new Point(picBoxPlayer.Location.X + picBoxPlayer.Width, picBoxBall.Location.Y);
                ballSpeedX = -ballSpeedX;
                playerCollision();
            }
        }
        private void playerCollision()
        {
            sounds["pong1"].Play();
            SlowDownBall();
        }
        private void aiCollision()
        {
            sounds["pong2"].Play();
            SlowDownBall();
        }
        private void sideCollision()
        {
            sounds["pong3"].Play();

            SpeedUpBall();
        }
        private void padlleSideCollision()
        {
            sounds["pong9"].Play();
        }
        private void SpeedUpBall()
        {
            if (ballSpeedY > 0)
            {
                ballSpeedY += ballIncreaseSpeedRate;
                if (ballSpeedY >= ballSpeedLimited)
                    ballSpeedY = ballSpeedLimited;
            }
            else
            {
                ballSpeedY -= ballIncreaseSpeedRate;
                if (ballSpeedY <= -ballSpeedLimited)
                    ballSpeedY = -ballSpeedLimited;
            }

            if (ballSpeedX > 0)
            {
                ballSpeedX += ballIncreaseSpeedRate;
                if (ballSpeedX >= ballSpeedLimited)
                    ballSpeedX = ballSpeedLimited;
            }
            else
            {
                ballSpeedX -= ballIncreaseSpeedRate;
                if (ballSpeedX <= -ballSpeedLimited)
                    ballSpeedX = -ballSpeedLimited;
            }
        }
        private void SlowDownBall()
        {
            if (ballSpeedY > 0)
            {
                ballSpeedY -= ballIncreaseSpeedRate;
                if (ballSpeedY <= ballStartSpeed)
                    ballSpeedY = ballStartSpeed;
            }
            else
            {
                ballSpeedY += ballIncreaseSpeedRate;
                if (ballSpeedY >= -ballStartSpeed)
                    ballSpeedY = -ballStartSpeed;
            }

            if (ballSpeedX > 0)
            {
                ballSpeedX -= ballIncreaseSpeedRate;
                if (ballSpeedX <= ballStartSpeed)
                    ballSpeedX = ballStartSpeed;
            }
            else
            {
                ballSpeedX += ballIncreaseSpeedRate;
                if (ballSpeedX >= -ballStartSpeed)
                    ballSpeedX = -ballStartSpeed;
            }
        }
    }
}

【问题讨论】:

  • 您是否修改了设计器生成的代码(Pong\Form1.Designer.cs)?
  • 你不会真的指望我们阅读所有的垃圾吧?删除所有不相关的代码。
  • 我注意到一件事,错误提示 Pong.Form1.Dispose(bool) 其中类名是 gameArea 你修改了类名吗?

标签: c# winforms


【解决方案1】:

正如 cmets 中所暗示的,更改 .cs 文件中的类名而不更新其他引用可能会破坏事情。您从Form1 类开始,将其中一个引用更改为gameArea,但没有更改其他引用。因此,您有 两个 类,gameArea 派生自 FormForm1 隐式派生自 objectForm1 包含 Dispose 方法,但 object 没有任何要覆盖的 Dispose 方法。

要解决此问题,首先将gameArea 改回Form1。那应该让您的代码再次编译。然后打开设计器,并使用它将Form1 重命名为gameArea。这将比您手动更新更多。

Hans Passant 在 cmets 中指出,也可以使用上下文菜单(在“重构”下)中的“重命名”选项来代替设计器。这将更新代码中的任何引用,而无需通过创建设计时表单并保存它的过程。因此,可能与在设计器中重命名所得到的结果略有不同,例如,如果您的代码访问表单的 Name 属性,但对于大多数程序来说,它应该足够好。

【讨论】:

  • 您可能需要提及右键单击 > 重构 > 重命名,作为从编辑器完成相同操作的方式。
  • @HansPassant 谢谢,但是测试一下,它实际上并没有做同样的事情。在一个空白表格上,我立即发现了不同之处:rename 不会更新设计器文件中的名称和 cmets。现在这些是无害的差异,代码仍然可以工作,但我不相信在更复杂的形式中没有其他重要的差异。
  • 它工作正常,代码序列化器只关心类型名称。
  • @HansPassant 把它放在我的回答中,谢谢。我希望我的措辞不会不必要地警告 OP 不要这样做,但同时仍指出潜在的差异。
  • 所以我将 public partial class gameArea : Form 更改为 public partial class Form1 : Form , public gameArea() 更改为 public Form1() 和 partial class Form1 更改为 partial class gameArea 。我尝试编译此代码,但它不起作用,编译时出现此错误:错误 CS0115:'Pong.gameArea.Dispose(bool)':找不到合适的方法来覆盖
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 2019-07-07
  • 2018-09-29
  • 2020-07-01
  • 1970-01-01
  • 2022-08-07
相关资源
最近更新 更多