XNA 4.0 - 窗口最小化时会发生什么?

发布于 2024-10-09 22:08:44 字数 1629 浏览 0 评论 0原文

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

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

ObjectDisposeException

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

怎么了?

I'm learning F#, and decided to try making simple XNA games for windows using F# (pure enthusiasm) , and got a window with some images showing up.

Here's the code:

(*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()

And the AddSprite Method:

   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)]

The _list object has a ContentManager, here's the constructor:

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

But I can't minimize the window, since when it regains its focus, i get this error:

ObjectDisposedException

Cannot access a disposed object.
Object name: 'GraphicsDevice'.

What is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-10-16 22:08:44

好吧,经过一段时间的努力,我终于开始工作了。但这似乎并不“正确”
(这样想,使用 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()

每当我的游戏需要 LoadContent 时,我都会调整 GraphicsDevice,并在 DrawSprites() 方法中检查纹理是否已释放,如果是的话,重新加载。

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

(代码看起来像 Initialize() 加载内容,然后 LoadContent() 初始化,但是哦,好吧)

Well after struggling for some time I got it to work. But it doesn't seem "right"
(thinking that way, using XNA and F# doesn't seem right either, but it's fun.)

(*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()

I adjust the graphicsDevice whenever my game needs to LoadContent, and in the DrawSprites() method I check if the texture is disposed, if it is, load it up again.

But this thing bugs me. I didn't know I had to Load all Content again everytime the window is minimized.

(And the code makes it look like Initialize() loads Content, and LoadContent() initializes, but oh well)

夏天碎花小短裙 2024-10-16 22:08:44

您所观察到的是正常行为,顺便说一下,它不是 F# 所特有的。请参阅 http://msdn.microsoft.com/ zh-cn/library/microsoft.xna.framework.game.loadcontent.aspx

该方法由Initialize调用。此外,每当需要重新加载游戏内容时(例如发生 DeviceReset 事件时)都会调用它。

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

What you are observing is normal behaviour, it's by the way not specific to F#. See http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.loadcontent.aspx

This method is called by Initialize. Also, it is called any time the game content needs to be reloaded, such as when the DeviceReset event occurs.

Are you loading all of your content in Game.LoadContent? If you do, you should not be getting these errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文