【问题标题】:Visual Studio says "Method must have a return type"Visual Studio 说“方法必须有返回类型”
【发布时间】:2014-03-30 03:30:54
【问题描述】:

上面写着

"方法必须有返回类型"

每当我尝试调试它时。

我不知道如何修复这个类

这是一个 c# 编码的 2d 游戏的玩家类

public class player
{
    public float moveSpeed;
    public Vector2 position;
    public Texture2D texture;

    //default constructer
    public Player(Texture2D tex, Vector2 startPos)
    {
        position  = startPos;
        texture   = tex;
        moveSpeed = 5.0f;
    }
    public void Update(GameTime gameTime)
    {
        //------------------------------------------
        //check for keyboard input(keyboard IF statements)

    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}

【问题讨论】:

  • 嗯,哪一行有错误?你不觉得告诉我们会有帮助吗?

标签: c# methods spritebatch


【解决方案1】:

您的类是player,但构造函数是Player,因为它们不同,所以期望Player 是方法而不是构造函数

把类名改成Player就好了

【讨论】:

    【解决方案2】:

    你的班级名称是player,小写。当编译器找到类Player(大写)的构造函数时,它认为这是一个名为Player的方法,没有指定返回类型。

    因此,只需将您的类重命名为大写 Player。 C# 区分大小写,所以playerPlayer 是两个不同的东西。

    【讨论】:

      【解决方案3】:

      你的类是小写的播放器,你的构造函数是大写的。类名和构造函数应该始终相同并且区分大小写:)

      【讨论】:

      • 我知道已经快 5 年了。你的*
      • @SharlSherif 永远不会迟到我想修正我的拼写!我改了,谢谢 :D
      【解决方案4】:

      我想这是一个错字。你的类名 player 是小写的,你的构造函数有一个大写字母,所以它不是一个 cinstructor 而是一个缺少 void 关键字的方法。

      编辑:很抱歉 se4ems 是一个重复的答案,似乎很多人几乎同时回答了;)

      【讨论】:

        【解决方案5】:

        你的类名是“player”,但构造函数名是“Player”(p的大写)。所以构造函数被认为是没有返回类型的方法。将其重命名为“播放器”,它将起作用:)

        【讨论】:

          猜你喜欢
          • 2015-05-16
          • 2012-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-29
          • 2015-12-15
          • 2020-02-16
          相关资源
          最近更新 更多