我正在制作一个小行星产生并向下移动屏幕的游戏。在游戏的更新方法中,我使用随机数来小心地产生小行星。当我启动它时,它开始在前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);
}
}
}
非常感谢所有帮助。 抱歉代码。我很难设置它。请帮助。
答案 0 :(得分:4)
正如Tobias所说,你可能应该把它放在Game Dev网站上,而你似乎只有一个Asteroid
被实例化了。在Game1对象中,您声明并实例化Asteroid
对象。然后在Update方法中,将其重复添加到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运行,则为1秒,对同一个asteroid
对象的最多900个引用位于objectList
,而第30帧asteroid
正在使用Update()
}和Draw()
方法调用最多900次。我很确定这是你滞后的根源。执行上面给出的更正将导致objectList
填充最多900个不同的 Asteroid
,但肯定会遇到延迟。您还需要做的是在任何给定时间(objectList
的长度只能是x个数量)和/或每次创建的小行星数量都会增加屏幕上小行星数量的限制。我会建议像
for (int i = 0; i < 5; i++)
{
if (objectList.Count < 50) // Maximum asteroids on screen
{
asteroid = new Asteroid();
objectList.Add(asteroid);
}
}
将导致时间仅为五个新小行星,在任何给定时间最多为50。如果你添加了破坏小行星的功能,你必须记住从objectList
删除它们。
编辑 - 正如Neomex所说,你的绘图也是一个问题。我建议在这里查看GDC 2008关于XNA Framework性能的演讲:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=6082 它简要介绍了一些绘图和计算性能以及您应该/不应该做的事情。
EDIT2-实际上,由于你的随机概率,最多可以有900个对象引用和调用,而不是完整的900。
答案 1 :(得分:3)
尝试尽可能少地进行首发通话,他们经常会减慢游戏速度。 (小行星类)
您在更新功能中显示图形非常糟糕。
如果这些失败,请尝试下载最新的驱动程序。
Reasuming:
小行星类的变化
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);
}
答案 2 :(得分:0)
正如Tobias已经指出的那样,每次想要生成一个新的星号时,你都使用相同的asteriod对象。您需要在对象列表中添加new Asteriod()
而不是使用相同的列表。
此外,此代码很可能是您在表现方面受到影响的原因。
if (rand.Next(0, 8) == 2)
{
for (int i = 0; i < 30; i++)
{
objectList.Add(asteroid);
}
}
在每次更新调用时,您都会将30个同一对象的实例添加到集合中(每秒约60次)。我意识到你使用的是一个随机数,但大约有12.5%的更新调用。
因此,在5-10秒内,您现在拥有数千个这样的对象,并且正在绘制相同的图像数千次。