Visual studio 2010 在f中找不到方法或对象构造函数

Visual studio 2010 在f中找不到方法或对象构造函数,visual-studio-2010,f#,xna,Visual Studio 2010,F#,Xna,在过去的几周里,我一直在努力学习f,在某些方面遇到了一些问题。我正在尝试将它与XNA结合使用,并且正在编写一个非常简单的游戏 我有一个简单的player类,它实现了DrawableGameComponent,然后重写了它的Draw、Update和LoadContent方法 type Player (game:Game) = inherit DrawableGameComponent(game) let game = game let mutabl

在过去的几周里,我一直在努力学习f,在某些方面遇到了一些问题。我正在尝试将它与XNA结合使用,并且正在编写一个非常简单的游戏

我有一个简单的player类,它实现了DrawableGameComponent,然后重写了它的Draw、Update和LoadContent方法

type Player (game:Game) =
        inherit DrawableGameComponent(game)

        let game = game
        let mutable position = new Vector2( float32(0), float32(0) )
        let mutable direction = 1
        let mutable speed = -0.1
        let mutable sprite:Texture2D = null

        override this.LoadContent() =
             sprite <- game.Content.Load<Texture2D>("Sprite")

        override this.Update gt= 
            if direction = -1 && this.Coliding then
                this.Bounce
            this.Jumping
            base.Update(gt)

        override this.Draw gt=
            let spriteBatch = new SpriteBatch(game.GraphicsDevice)
            spriteBatch.Begin()
            spriteBatch.Draw(sprite, position, Color.White)
            spriteBatch.End()
            base.Draw(gt)
等等

然后,主游戏类生成一个新的玩家对象等

module Game=

    type XnaGame() as this =
        inherit Game()

        do this.Content.RootDirectory <- "XnaGameContent"
        let graphicsDeviceManager = new GraphicsDeviceManager(this)

        let mutable player:Player = new Player(this)
        let mutable spriteBatch : SpriteBatch = null
        let mutable x = 0.f
        let mutable y = 0.f
        let mutable dx = 4.f
        let mutable dy = 4.f

        override game.Initialize() =
            graphicsDeviceManager.GraphicsProfile <- GraphicsProfile.HiDef
            graphicsDeviceManager.PreferredBackBufferWidth <- 640
            graphicsDeviceManager.PreferredBackBufferHeight <- 480
            graphicsDeviceManager.ApplyChanges() 
            spriteBatch <- new SpriteBatch(game.GraphicsDevice)
            base.Initialize()

        override game.LoadContent() =
            player.LoadContent () //PROBLEM IS HERE!!!
            this.Components.Add(player)

        override game.Update gameTime = 
            player.Update gameTime


        override game.Draw gameTime = 
            game.GraphicsDevice.Clear(Color.CornflowerBlue)
            player.Draw gameTime
编译器报告一个错误,称未找到方法或对象构造函数LoadContent

我觉得这很奇怪,因为绘图和更新工作正常,并且是由intellisense发现的,但不是LoadContent

这可能只是我犯的一个非常愚蠢的错误,但如果有人发现这个问题,我将不胜感激

谢谢

受保护-因此您无法从XnaGame类调用它

我不清楚最终调用它意味着什么,但显然您不应该自己直接调用它。

受保护-因此您无法从XnaGame类调用它


我不清楚到底该怎么称呼它,但显然你不应该自己直接称呼它。

这个错误听起来确实令人困惑。您正在覆盖播放器类型定义中的LoadContent成员,但正如Jon指出的,该成员受到保护。F不允许您使成员更可见,因此即使您的定义仍然受保护,您通常也无法在F中定义受保护的成员,因此错误消息很差

您可以通过添加从播放机内部调用LoadContent的其他成员来解决此问题:

…然后LoadContent成员仍将受到保护,无法从外部访问,但新的LoadContentPublic成员将是public这是F中成员的默认值,您应该能够从XnaGame调用它


然而,正如Jon所指出的——也许你不应该自己调用这个方法,因为XNA运行时会在需要时自动调用它。

这个错误听起来确实令人困惑。您正在覆盖播放器类型定义中的LoadContent成员,但正如Jon指出的,该成员受到保护。F不允许您使成员更可见,因此即使您的定义仍然受保护,您通常也无法在F中定义受保护的成员,因此错误消息很差

您可以通过添加从播放机内部调用LoadContent的其他成员来解决此问题:

…然后LoadContent成员仍将受到保护,无法从外部访问,但新的LoadContentPublic成员将是public这是F中成员的默认值,您应该能够从XnaGame调用它


然而,正如Jon所指出的,也许你不应该自己调用这个方法,因为XNA运行时会在需要时自动调用它。

谢谢!这完全有道理,谢谢你指出这一点!那么,如果我只是将我的对象添加到游戏组件中,那么它应该被自动调用吗?再次感谢@EwenCluley:老实说,我不知道——我想是的,但我自己没有做过任何XNA的工作。谢谢!这完全有道理,谢谢你指出这一点!那么,如果我只是将我的对象添加到游戏组件中,那么它应该被自动调用吗?再次感谢@EwenCluley:老实说,我不知道——我想是的,但我自己没有做过任何XNA的工作。很好,非常有用和深入的回答。thansk对于这一点是有意义的-我希望在c中我可以改变重写方法的可见性。。。我想。。。奇怪的是f不允许这样。谢谢你的帮助!很好,非常有用和深刻的答案。thansk对于这一点是有意义的-我希望在c中我可以改变重写方法的可见性。。。我想。。。奇怪的是f不允许这样。谢谢你的帮助!
override this.LoadContent() = 
    sprite <- game.Content.Load<Texture2D>("Sprite") 
member this.LoadContentPublic() = 
    this.LoadContent()