2

我正在学习 F#,并决定尝试使用 F#(纯粹的热情)为 windows 制作简单的 XNA 游戏,并得到一个显示一些图像的窗口。

这是代码:

(*Methods*)     
member self.DrawSprites() = 
    _spriteBatch.Begin()
    for i = 0 to _list.Length-1 do
        let spentity = _list.List.ElementAt(i)
        _spriteBatch.Draw(spentity.ImageTexture,new Rectangle(100,100,(int)spentity.Width,(int)spentity.Height),Color.White)      
    _spriteBatch.End()

(*Overriding*)   
override self.Initialize() =
    ChangeGraphicsProfile()                              
    _graphicsDevice <- _graphics.GraphicsDevice
    _list.AddSprite(0,"NagatoYuki",992.0,990.0)
    base.Initialize() 

override self.LoadContent() =         
    _spriteBatch <- new SpriteBatch(_graphicsDevice)
    base.LoadContent()

override self.Draw(gameTime : GameTime) =
    base.Draw(gameTime)
    _graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
    self.DrawSprites()

AddSprite方法:

   member self.AddSprite(ID : int,imageTexture : string , width : float, height : float) = 
      let texture = content.Load<Texture2D>(imageTexture)
      list <- list @ [new SpriteEntity(ID,list.Length, texture,Vector2.Zero,width,height)]

_list 对象有一个ContentManager,这是构造函数

   type SpriteList(_content : ContentManager byref) =
      let mutable content = _content
      let mutable list = []

但我无法最小化窗口,因为当它重新获得焦点时,我收到此错误:

ObjectDisposedException

无法访问已处置的对象。
对象名称:“图形设备”。

怎么了?

4

2 回答 2

1

好吧,在挣扎了一段时间后,我开始工作了。但这似乎并不“正确”(这样想,使用 XNA 和 F# 似乎也不正确,但这很有趣。)

(*Methods*)     
member self.DrawSprites() = 
    _spriteBatch.Begin()
    for i = 0 to _list.Length-1 do
        let spentity = _list.List.ElementAt(i)
        if spentity.ImageTexture.IsDisposed then
            spentity.ImageTexture <- _list.Content.Load<Texture2D>(spentity.Name)
        _spriteBatch.Draw(spentity.ImageTexture,new Rectangle(100,100,(int)spentity.Width,(int)spentity.Height),Color.White)      
    _spriteBatch.End()

(*Overriding*)   
override self.Initialize() =
    ChangeGraphicsProfile()           
    _list.AddSprite(0,"NagatoYuki",992.0,990.0)
    base.Initialize() 

override self.LoadContent() =   
    ChangeGraphicsProfile()           
    _graphicsDevice <- _graphics.GraphicsDevice
    _spriteBatch <- new SpriteBatch(_graphicsDevice)
    base.LoadContent()

每当我的游戏需要加载内容时,我都会调整 graphicsDevice,并在 DrawSprites() 方法中检查纹理是否已处理,如果是,则再次加载它。

但这件事让我很烦。我不知道每次最小化窗口时我都必须再次加载所有内容。

(并且代码使它看起来像 Initialize() 加载内容,并且 LoadContent() 初始化,但是哦,好吧)

于 2010-12-30T18:49:39.430 回答
0

您观察到的是正常行为,顺便说一句,它并非特定于 F#。请参阅http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.loadcontent.aspx

此方法由 Initialize 调用。此外,在需要重新加载游戏内容时调用它,例如发生 DeviceReset 事件时。

您是否在 Game.LoadContent 中加载所有内容?如果你这样做,你不应该得到这些错误。

于 2011-01-03T20:00:35.027 回答