3

So I've created an Arkanoid Paddle which basically moves left and right with the analog stick.

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 is an initial integer value set at zero and when the Player presses the Right Trigger, it sets the value to 5, thus increasing the speed of the paddle.

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

What I want to do is make it so that after pressing the trigger, the value of speedup1 returns to 0 after a set interval of time and the player is not able to change it again for another set interval of time.

Help, please,

pinckerman
  • 4,115
  • 6
  • 33
  • 42
  • To put it simply and general, when making a game, you usually want an update part of your game loop where you send in the elapsed time (in ticks or milliseconds) each loop. This time is then used in the updates of all the entities to calculate their travel distance etc. This time can also be used to deduct from timers, such as cooldowns. This should all be done in the Update method of the game entity. – Tobberoth Nov 19 '13 at 07:15

3 Answers3

0

I don't know how your game is build up, you need to post more code for that, but generally speaking:

Now imagine a cooldown for something in game. The player pressed "a", some cool action happened and although he may press "a" again, nothing will happen for the next 5 seconds. But the game still runs and does all the other things that may happen ingame. This is not a conventional timer. It's a variable, lets call it ActionCooldown, and once the player triggers the action, it's set to 5 seconds. Every time the world changes, the timePassed is subtracted from that number until it's zero. All the time, the game is running and handling input and rendering. But only once ActionCooldown hits zero, another press of "a" will trigger that action again.

Quoted from here.

Create a variable. Set it to your cooldown value. Subtract time from it until it hits zero. Cooldown over.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

Try something like this:

// 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;
}
rhughes
  • 9,257
  • 11
  • 59
  • 87
  • Thanks for the quick reply but I don't really understand how to add code to stop the player from spamming RT all the time to keep getting that speed boost. Also, I tried putting the slow down code in the public Update() function but it's not allowing me to. – user3007548 Nov 19 '13 at 06:56
  • What do you mean 'not allowing me to'? – rhughes Nov 19 '13 at 07:07
0

Something like this, such that when the user presses RT, the next RT will have no effect until the first cool down has finished. So:

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;
    }        
}
Colton
  • 1,297
  • 14
  • 27