我正在尝试创建一个计时器,例如,在15秒内每隔3秒执行一个动作。
我尝试使用gameTime.ElapsedGameTime.TotalSeconds和循环,但不幸的是它不起作用。
我有一个Attack ()函数,当敌人攻击它时,它会减少玩家的统计数据。我希望在一个特定的敌人的情况下,这个函数在指定的时间内会减去玩家的生命值,例如每3秒。我想应该是在更新函数中访问gameTime,不幸的是,我不知道该怎么做。
public override Stats Attack()
{
attack = true;
return new Stats(0, -stats.Damage, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
public override void Update(GameTime gameTime)
{
spriteDirection = Vector2.Zero; // reset input
Move(Direction); // gets the state of my keyborad
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; // make movement framerate independant
spriteDirection *= Speed; // add hero's speed to movement
position += (spriteDirection * deltaTime); // adding deltaTime to stabilize movement
totalPosition = new Vector2((int)((BottomBoundingBox.Center.X) / 32.0f), (int)((BottomBoundingBox.Center.Y) / 32.0f));
base.Update(gameTime);
}
发布于 2019-06-03 08:38:22
我在我的游戏中使用这个:
Public Async Function DelayTask(Time As Double) As Threading.Tasks.Task
Await Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(Time))
End Function
已转换为C#:
public async System.Threading.Tasks.Task DelayTask(double Time)
{
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(Time));
}
您可以像这样在异步函数中使用它:
Await DelayTask(1.5);
此数字以秒为单位,您可以通过更改TimeSpan.whateverformat来更改此数字。
考虑到你会有各种各样的东西影响你的统计数据,也许你最好在你的stats类中有一个更新子例程,它将检查一个计划在某个时间点之后更新的效果列表。这将比让每个效果依赖于其自己的线程更好地提高性能。
https://stackoverflow.com/questions/56293634
复制相似问题