XNA 4.0 - 使用spriteBatch和basicEffect滞后

时间:2010-12-16 04:57:09

标签: c# xna draw lag xna-4.0

我目前正在开发一款游戏,我们需要结合使用 DrawUserIndexedPrimitives 和普通的 spriteBatch.Draw 。因为我们同时使用它们没有组合,但我们首先必须使用spriteBatch绘制一些2d精灵,然后我们禁用spriteBatch以启用basicEffect并绘制基元,最后再次启用spriteBatch。下面的代码显示了问题发生的代码部分。

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));
                levelController.Draw(spriteBatch);


                if (gameReady)
                {
                    foreach (GameObject.GameObject go in gameObjects)
                    {
                        go.Draw(spriteBatch);
                    }
                    spriteBatch.End();

                    foreach (GameObject.GameObject go in gameObjects)
                    {
                        if (go is GameObject.Enemy.Enemy)
                        {
                            GameObject.Enemy.Enemy enemy = (GameObject.Enemy.Enemy)go;

                            basicEffect = new BasicEffect(graphics.GraphicsDevice);
                            basicEffect.VertexColorEnabled = true;
                            basicEffect.CurrentTechnique.Passes[0].Apply();

                            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1);
                        }
                    }
                }

                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));

如果下面的代码被引用,滞后就会停止。

basicEffect = new BasicEffect(graphics.GraphicsDevice);
                        basicEffect.VertexColorEnabled = true;
                        basicEffect.CurrentTechnique.Passes[0].Apply();

                        GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1)

真的可能是我们不能在没有游戏滞后的情况下使用spriteBatch和basicEffect吗?它已经在3台不同的计算机上进行了测试,从一台非常旧的笔记本电脑到一台全新的游戏机。 游戏因发生滞后而无法播放。

1 个答案:

答案 0 :(得分:3)

我认为你应该在绘图程序的其他地方创建basiceffect。如果我猜对了,你会一直使用相同的basiceffect,所以把它放在inicializing中,因为每个帧的“new”(编辑:在每个对象的foreach中)都会降低成本。

相关问题