我正在尝试使用 F# 并想看看是否可以在 F# 中使用 Monogame 进行一些简单的操作。我认为从 C# 到 F# 的转换应该是直接的,但到目前为止并非如此。我目前所拥有的代码只是一个简单的空项目,应该可以运行。或者至少在 C# 中可以。然而,在 F# 中运行这个代码会产生一个。
Unhandled exception. System.InvalidOperationException: No Graphics Device Service
我现在的代码如下,其实并没有什么特别的。由于必须在“LoadContent”中实例化spritebatch,所以我不得不使用可变val来表示它。有没有人能指出我做错了什么?我将不胜感激。
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
member this.Gfx = new GraphicsDeviceManager(this)
每次访问this.Gfx
都会创建一个新的GraphicsDeviceManager
。这可能不是一个好的做法。 - Asti