【问题标题】:Game lagging (beginner)游戏滞后(初学者)
【发布时间】:2012-01-22 07:03:13
【问题描述】:

我正在创建一个游戏,其中小行星会在屏幕上产生并向下移动。在游戏的更新方法中,我使用一个随机数来零星地产生小行星。当我启动它时,它在前 5 秒内开始滞后。我可以看到这一点,因为分数计数器(每个刻度都会上升)以 30 的间隔开始运行。此外,小行星的图像甚至没有出现。

这里是gameObject类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;  
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;  
using Microsoft.Xna.Framework.Storage;


namespace thedodger
{
public abstract class gameObject
{
    public static Texture2D texture;
    public Rectangle rectangle;

    public abstract void Draw(SpriteBatch spriteBatch);
    public abstract void Update(GameTime gameTime);
}
} 

这里是小行星类;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace thedodger
{
public class Asteroid : gameObject
{
    Random rand = new Random(1);
    int yPos = -10;

    public override void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(texture, new Rectangle(rand.Next(32,400),      yPos,32,32),Color.White);
        spriteBatch.End();

    }

    public override void Update(GameTime gameTime)
    {
        yPos--;


    }
}
}

这里是 game1 类:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace thedodger
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    int scorevalue = 0;
    SpriteFont font;
    player player1 = new player();
    List<gameObject> objectList = new List<gameObject>();
    Random rand = new Random(1);
    Asteroid asteroid = new Asteroid();


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        font = Content.Load<SpriteFont>("font");
        //player1.image = Content.Load<Texture2D>("EnemyShip005.png");
        gameObject.texture = Content.Load<Texture2D>("asteroid");

        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        scorevalue++;

        if (rand.Next(0, 8) == 2)
        {
            for (int i = 0; i < 30; i++)
            {

                objectList.Add(asteroid);


            }
        }

        foreach (Asteroid asteroid in objectList)
        {
            asteroid.Update(gameTime);
            asteroid.Draw(spriteBatch);
        }



        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
         spriteBatch.Begin();
         spriteBatch.DrawString(font, "Score: " + scorevalue, new Vector2(5, 5), Color.White);
         spriteBatch.End();



        base.Draw(gameTime);
    }
}
}

所有帮助将不胜感激。 对不起代码。我很难设置它。也请帮忙。

【问题讨论】:

  • 这看起来像是game development site 的问题。可能有比这里更多的 XNA 专家。我发现的第一个错误:你总是有一个 Asteroid 对象,它更新并且每帧绘制 30 次的倍数。

标签: c# xna lag


【解决方案1】:

正如 Tobias 所说,您可能应该将它放在 Game Dev 网站上,但您似乎只实例化了一个 Asteroid。在 Game1 对象中,您声明并实例化了一个 Asteroid 对象。然后在更新方法中将它重复添加到objectList。为了得到我认为你想要的,你应该改变

Asteroid asteroid = new Asteroid();

Asteroid asteroid;

然后改变

for (int i = 0; i < 30; i++)
{
    objectList.Add(asteroid);
}

for (int i = 0; i < 30; i++)
{
    asteroid = new Asteroid();
    objectList.Add(asteroid);
}

在您的原始代码中,您将asteroid 声明并实例化为特定的Asteroid,但永远不要更改它。因此,在整个程序中asteroid 指向Asteroid 的一个特定实例。然后你反复将asteroid 添加到objectList。因此,在每一帧之后,对同一 asteroid 对象的 30 个新引用将被添加到 objectList 中,并且 Update()Draw() 方法将在 asteroid 上被调用,以获取 objectList 中对它的每个引用。因此,在 30 帧之后,如果以 30FPS 运行,则为一秒,在 objectList 中最多有 900 个对相同 asteroid 对象的引用,并且在第 30 帧 asteroid 正在调用其 Update()Draw() 方法到 900 次。我很确定这是你滞后的根源。执行上面给出的更正将导致objectList 填充多达 900 个不同 Asteroids,但肯定也会出现延迟。您还需要做的是在任何给定时间为屏幕上的小行星数量添加限制(objectList 的长度只能是 x 数量)和/或降低每次创建的小行星数量。我会建议像

for (int i = 0; i < 5; i++)
{
    if (objectList.Count < 50) // Maximum asteroids on screen
    {
        asteroid = new Asteroid();
        objectList.Add(asteroid);
    }
}

在一段时间内只会产生 5 颗新的小行星,在任何给定时间最多会产生 50 颗。但是,如果您添加了摧毁小行星的功能,您必须记住将它们从 objectList 中删除。

编辑- 正如 Neomex 所说,您的绘图也是一个问题。我建议在这里查看 GDC 2008 关于 XNA 框架性能的演讲:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=6082 它非常简要地介绍了一些绘图和计算性能以及您应该/不应该做什么。

EDIT2- 实际上,由于您的随机概率,最多有 900 个对象引用和调用,而不是完整的 900 个。

【讨论】:

    【解决方案2】:

    尽量减少开始结束的调用,它们通常会大大减慢游戏速度。 (小行星类)

    您在更新功能中显示图形,这真的很糟糕。

    如果这些都失败,请尝试下载最新的驱动程序。

    复习:

    改变你的小行星类别

    public override void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(texture, new Rectangle(rand.Next(32,400),      yPos,32,32),Color.White);
        spriteBatch.End();
    
    }
    

    public override void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, new Rectangle(rand.Next(32,400),      yPos,32,32),Color.White);
    }
    

    现在在你的更新功能中

    foreach (Asteroid asteroid in objectList)
    {
        asteroid.Update(gameTime);
        asteroid.Draw(spriteBatch);
    }
    

    foreach (Asteroid asteroid in objectList)
    {
        asteroid.Update(gameTime);
    }
    

    并在绘图函数中添加:

    foreach (Asteroid asteroid in objectList)
    {
        asteroid.Draw(spriteBatch);
    }
    

    【讨论】:

    • 补充一点 @Neomex 所说的,你的 draw 和 update 调用可能不一定是 1:1 的关系。这就是为什么您应该将抽奖与更新分开。
    【解决方案3】:

    正如 Tobias 已经指出的那样,每次您想要生成一个新的小行星时,您都在使用同一个小行星对象。您需要将new Asteriod() 添加到对象列表中,而不是使用相同的。

    此外,这段代码很可能是您在性能方面受到影响的原因。

        if (rand.Next(0, 8) == 2)
        {
            for (int i = 0; i < 30; i++)
            {
    
                objectList.Add(asteroid);
    
    
            }
        }
    

    您在每次更新调用时将同一对象的 30 个实例添加到集合中(大约每秒 60 次)。我意识到您使用的是随机数,但它仍然发生在大约 12.5% 的更新调用中。

    因此,在 5-10 秒内,您现在拥有数千个这样的对象,并且正在绘制相同的图像数千次。

    【讨论】:

    • 谢谢你们。这会很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2020-01-29
    • 1970-01-01
    • 2012-07-13
    • 2021-03-12
    • 2014-06-13
    相关资源
    最近更新 更多