我正在开发一款2D赛车游戏。这是XNA游戏的截图; http://oi41.tinypic.com/vxhy5y.jpg。我想知道如何创建一个计时器。因此,例如,“如果car1blue / car2red与方格旗启动计时器碰撞,如果它们再次发生碰撞,则停止计时器。”这会在理论上起作用吗?如果它在理论上有用,我该如何在XNA中进行?
如果你愿意,我可以提供我迄今为止所做的代码。
非常感谢!
EDIT; 是否有可能告诉我代码在哪里?
答案 0 :(得分:0)
如果不知道如何将它们放在一起,你能不能只在它们通过线路的某个时间存储一个DateTime,所以当它开始时你会这样做:
public MyGame
{
DateTime lastLapTimestamp = DateTime.Now;
TimeSpan lastLapTime = new TimeSpan();
// other stuff
public void Update(TimeSpan elapsedTime)
{
if(HasCompletedLap)
{
var currentLapTimestamp = DateTime.Now;
lastLapTime = currentLapTimestamp - lastLapTimestamp;
lastLapTimestamp = currentLaptimeStamp;
}
// Other stuff
}
}
然后只需显示你的lastLapTime,无论你想看看它需要多长时间。
答案 1 :(得分:0)
这里要求的是您的示例空XNA项目中需要的实现,我再次回答您在上面提出的关于单圈时间的问题:
/*
NEW MEMBERS FOR YOU TO IMAGINE YOU HAVE SOME MEANINGFUL RACING GAME
*/
private Vector2 yourPosition;
private Texture2D yourCar;
private Texture2D yourTrack;
private Rectangle yourStartingLine;
private DateTime currentLapTimestamp = DateTime.Now();
private TimeSpan currentLapTime = new TimeSpan();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// Put your starting line somewhere
yourStartingLine = new Rectangle(100,100,10,100)
base.Initialize();
}
protected override void LoadContent()
{
yourCar = Content.Load<Texture2D>("YourCarAssetName")
yourTrack = Content.Load<Texture2D>("YourTrackAssetName")
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Check your keyboard input
UpdatePosition(Keyboard.GetState());
if(HasPassedStartingLine())
{ UpdateLapTime(); }
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawTrack();
DrawCar();
DrawLapTime();
base.Draw(gameTime);
}
/*
NEW METHODS FOR YOU TO PRETEND DO STUFF
*/
private void UpdateLapTime()
{
var newTimestamp = DateTime.Now;
currentLapTime = newTimestamp - currentLapTimestamp;
}
private bool HasPassedStartingLine()
{
// Work out if you have passed the starting line
// and make sure you dont register it again until
// they have passed the finish line
}
private void UpdatePosition(KeyboardState keyboardState)
{
// Assumes you work out how to handle your players
// controls to move the position of the car
}
private void DrawTrack()
{
// Your rendering logic for the track
}
private void DrawCar()
{
// Your rendering logic for the car
}
private void DrawLapTime()
{
// your rendering logic for the lap time
}