[英]Issue Running Monogame in F#
我正在对 F# 进行一些试验,我想看看是否可以在 F# 中使用 Monogame 做一些简单的事情。 我认为从 C# 到 f# 的翻译很简单,但到目前为止还没有。 我到目前为止的代码只是一个应该运行的简单空项目。 或者至少它会在 C# 中。 但是在 F# 中运行它会产生
Unhandled exception. System.InvalidOperationException: No Graphics Device Service
我的代码是这样的,它真的没有做任何疯狂的事情。 我被迫为 spritebatch 使用可变 val,因为无论出于何种原因,您都必须在 LoadContent 中实例化它。 有人对我做错了什么有任何指示吗? 我将不胜感激。
type GameState =
inherit Game
new() = { inherit Game(); Sb = null; }
member this.Gfx : GraphicsDeviceManager = new GraphicsDeviceManager(this)
val mutable Sb : SpriteBatch
override this.Initialize() =
this.Content.RootDirectory <- "Content"
this.IsMouseVisible <- false
base.Initialize ()
override this.LoadContent () =
this.Sb <- new SpriteBatch(this.Gfx.GraphicsDevice)
base.LoadContent ()
override this.UnloadContent () =
base.UnloadContent ()
override this.Update (gameTime : GameTime) =
base.Update (gameTime)
override this.Draw (gameTime : GameTime) =
this.Gfx.GraphicsDevice.Clear (Color.CornflowerBlue)
this.Sb.Begin()
//draw here
this.Sb.End()
base.Draw (gameTime)
[<EntryPoint>]
let main argv =
let gs = new GameState()
gs.Run()
0 // return an integer exit code
Asti 是正确的,您不想重复创建新的GraphicsDeviceManager
。
这是一些对您所做的更改最少的工作代码。 请注意,要在构造函数时定义值,您需要在类型名称后面加上()
。 对SpriteBatch
使用mutable
很难看,但在这种情况下很常见,您不需要将其设为成员:
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
type GameState() as this =
inherit Game()
let gfx = new GraphicsDeviceManager(this)
let mutable sb = Unchecked.defaultof<SpriteBatch>
override this.Initialize() =
this.Content.RootDirectory <- "Content"
this.IsMouseVisible <- false
base.Initialize ()
override this.LoadContent () =
sb <- new SpriteBatch(gfx.GraphicsDevice)
base.LoadContent ()
override this.UnloadContent () =
base.UnloadContent ()
override this.Update (gameTime : GameTime) =
base.Update (gameTime)
override this.Draw (gameTime : GameTime) =
gfx.GraphicsDevice.Clear (Color.CornflowerBlue)
sb.Begin()
//draw here
sb.End()
base.Draw (gameTime)
[<EntryPoint>]
let main argv =
let gs = new GameState()
gs.Run()
0 //
随意看看我的这个 repo ,它给出了一个使用 MonoGame 和 F# 的工作示例(尽管它现在可能有点过时了),包括基本的内容管道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.