为技能使用添加冷却

时间:2013-11-19 06:27:55

标签: c# xna xna-4.0

所以我创造了一个Arkanoid桨,基本上用模拟棒左右移动。

if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X == -1.0f)
{
    // Player one has pressed the left thumbstick up.
    Position.X = Position.X - (5 + speedup1);
}


if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X == 1.0f)
{
    // Player one has pressed the left thumbstick up.
    Position.X = Position.X + (5 + speedup1);
}

speedup1是设置为零的初始整数值,当Player按下右触发器时,它将值设置为5,从而提高了桨的速度。

if (GamePad.GetState(PlayerIndex.One).Triggers.Right > 0)
{
    speedup1 = 5;
}

我想要做的就是按下触发器后,speedup1的值在设定的时间间隔后返回0,并且播放器无法再次更改为另一个设定的时间间隔时间。

请帮助,

3 个答案:

答案 0 :(得分:0)

我不知道你的游戏是如何构建的,你需要为此发布更多代码,但一般来说:

  

现在想象一下游戏中某事的冷却时间。玩家按下“a”,   发生了一些很酷的动作,虽然他可能再次按“a”,但没有   将在接下来的5秒内发生。但游戏仍在运行   所有其他可能发生的事情。这不是一个   传统计时器。它是一个变量,我们称之为ActionCooldown,和   一旦玩家触发动作,它就会设置为5秒。每次   世界变化,从该数字中减去timePassed直到   它是零。游戏一直在运行并处理输入和   渲染。但只有一次ActionCooldown击中零,另一次按下   “a”将再次触发该行动。

引自here

创建变量。将其设置为您的冷却时间值。从中减去时间直到它达到零。冷却时间结束。

答案 1 :(得分:0)

尝试这样的事情:

// declare this as a variable in your game class
public float PaddleSpeedUpCoolDownTime = 0.0f;

if (GamePad.GetState(PlayerIndex.One).Triggers.Right > 0)
{
    PaddleSpeedUpCoolDownTime = 5.0f; // 5 seconds
    speedup1 = 5;
}

// in your main game loop in an appropriate place
if (PaddleSpeedUpCoolDownTime > 0.0f)
{
    speedup1 = speedup1 * 0.9f * frameTime; // to slow down gradually

    PaddleSpeedUpCoolDownTime = PaddleSpeedUpCoolDownTime - frameTime;
}

答案 2 :(得分:0)

这样的事情,当用户按下RT时,下一个RT将在第一次冷却完成后无效。所以:

bool boosting = false;
bool coolingDown = false;
float someIncrement = -1;
float cooldownTime = 0;
float timeRequired = 10;
public void updateMovement()
{
    if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X == -1.0f)
    {
        // Player one has pressed the left thumbstick up.
        Position.X = Position.X - (5 + speedup1);
    }

    if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X == 1.0f)
    {
        // Player one has pressed the left thumbstick up.
        Position.X = Position.X + (5 + speedup1);
    }        

    //updates boost speed, then once boostspeed returns to normal,
    //begin to update cooldown time
    if(boosting && speedup1 >= 0)
    { 
        speedup1 -= someIncrement;
        if(speedup1 <= 0)
        {
            speedup1 = 0
            boosting = false;
            coolingDown = true;
        }
    }

    //executes once boost has returned to normal
    //updates time remaining before you can boost again
    if(coolingDown)
    {
        cooldownTime += gameTime.EllapsedMilliseconds;
        if(cooldownTime >= timeRequired)
        {
            coolingDown = false;
            cooldownTime = 0;
        }
    }

    //will not execute until the boost has finished AND cooldown has finished
    if (GamePad.GetState(PlayerIndex.One).Triggers.Right > 0 
        && boosting == false && coolingDown == false)
    {
        speedup1 = 5;
        boosting = true;
    }        
}