2013-04-04 76 views
0

我正在使用this tutorial来学习一点XNA,我不断遇到问题。我不得不转换很多代码,因为它看起来教程不使用XNA 4.0。XNA 4.0 InvalidOperationException是unhandeled

但让我们开始追逐!

float aXPosition = (float)(-mCarWidth/2 + mCarPosition.X + aMove * Math.Cos(mCarRotation)); 
      float aYPosition = (float)(-mCarHeight/2 + mCarPosition.Y + aMove * Math.Sin(mCarRotation)); 
      Texture2D aCollisionCheck = CreateCollisionTexture(aXPosition, aYPosition); 

      //Bruke GetData til å fylle en array med fargen på pixlene ved collisons texturen 
      int aPixels = mCarWidth * mCarHeight; 
      Color[] myColors = new Color[aPixels]; 
      aCollisionCheck.GetData<Color>(0, new Rectangle((int)(aCollisionCheck.Width/2 - mCarWidth/2), 
       (int)(aCollisionCheck.Height/2 - mCarHeight/2), mCarWidth, mCarHeight), myColors, 0, aPixels); 

我得到当我尝试调试代码的错误说:InvalidOperationException异常被unhandeled,渲染目标必须在它被用作纹理无法在设备上设置。

我不知道该怎么做。

+0

发布这样的错误时,有助于指出抛出错误的确切代码行! – 2013-04-04 11:31:23

回答

2

这基本上意味着它所说的。

您必须通过调用GraphicsDevice.SetRenderTarget(null)(或将其设置为不同的渲染目标)来从设备中取消设置渲染目标。因为不能同时用它作为源纹理和目标缓冲区。

请记住,在这个版本的XNA中,没有ResolveRenderTarget。渲染目标只需就是纹理。


请注意,您正在使用的教程是非常可怕的。从这样的渲染目标回读非常缓慢。特别是可以很容易地在CPU上有效地完成它正在使用渲染目标(在变换区域中选择像素)的操作。考虑使用this better, official example

+0

啊,非常感谢! 这给了我更多的见解! :) – 2013-04-04 15:41:19