在 F# 中发布 运行 Monogame
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
。
这是一些工作代码,对您的代码进行了极小的改动。请注意,要在构造函数时定义值,您需要在类型名称后加上 ()
。使用 mutable
作为 SpriteBatch
是丑陋的,但在这种情况下很常见,你不需要让它成为成员:
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 //
请随时查看我的 this repo,其中提供了将 MonoGame 与 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
。
这是一些工作代码,对您的代码进行了极小的改动。请注意,要在构造函数时定义值,您需要在类型名称后加上 ()
。使用 mutable
作为 SpriteBatch
是丑陋的,但在这种情况下很常见,你不需要让它成为成员:
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 //
请随时查看我的 this repo,其中提供了将 MonoGame 与 F# 结合使用的工作示例(尽管它现在可能有点过时),包括基本内容管道。