【问题标题】:Method or object constructor not found in f#在 f# 中找不到方法或对象构造函数
【发布时间】:2011-09-26 21:41:27
【问题描述】:

过去几周我一直在尝试学习 f#,但在某些方面遇到了一些麻烦。我正在尝试将它与 XNA 一起使用,并且正在编写一个非常简单的游戏。

我有一个简单的播放器类,它实现了 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)

等等……

然后主 Game 类创建一个新的玩家对象等。

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”

我觉得这很奇怪,因为 Draw 和 Update 都可以正常工作,并且可以被智能感知找到,但 LoadContent 却没有!

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

谢谢

【问题讨论】:

    标签: visual-studio-2010 f# xna


    【解决方案1】:

    DrawableGameComponent.LoadContent 受到保护 - 因此您无权从您的 XnaGame 类中调用它。

    我不清楚最终调用它是什么意思,但显然你不应该自己直接调用它。

    【讨论】:

    • 谢谢!这完全有道理,感谢您指出这一点!所以如果我只是将我的对象添加到游戏组件中,那么它应该被自动调用吗?再次感谢!
    • @EwenCluley:说实话,我不知道——我怀疑是这样,但我自己没有做过任何 XNA 工作。
    【解决方案2】:

    这个错误听起来确实令人困惑。您在 Player 类型的定义中覆盖了 LoadContent 成员,但是(正如 Jon 指出的)该成员是 protected。 F# 不允许您使成员更可见,因此即使您的定义仍然是 protected(您通常不能在 F# 中定义 protected 成员,所以这就是错误消息很差的原因)。

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

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

    ...那么LoadContent 成员仍将是protected(无法从外部访问),但新的LoadContentPublic 成员将是公共的(这是F# 中member 的默认值)并且您应该是可以从您的XnaGame 调用它。

    但是,正如 Jon 指出的那样 - 也许您不应该自己调用该方法,因为 XNA 运行时会在需要时自动调用它。

    【讨论】:

    • 很好,非常有用和深入的答案。谢谢,这是有道理的-我想回忆一下在c#中我可以更改被覆盖方法的可见性...我认为...奇怪的是f#不允许这样做。感谢您的帮助!
    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    相关资源
    最近更新 更多