【问题标题】:XNA 4.0 - What happens when the window is minimized?XNA 4.0 - 当窗口最小化时会发生什么?
【发布时间】:2010-12-30 16:36:19
【问题描述】:

我正在学习 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

无法访问已处置的对象。
对象名称:'GraphicsDevice'。

发生了什么?

【问题讨论】:

  • 在我看来,当我最小化窗口时,XNA 会自动处理 GraphicsDevice。即使,当我重新加载它时,我得到了同样的错误,但对象名称:'Texture2D'。我尝试重新加载纹理,这也不起作用。我迷失了这个

标签: f# xna xna-4.0 c#-to-f#


【解决方案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() 初始化,但是哦)

【讨论】:

  • 你正在用 F# 制作凉宫春日游戏?想要
  • 我只是将我的引擎转换成另一种语言......但谁知道呢?我喜欢动漫。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多