Question

I need to make it so that when my boolean value changes to whatever it wasn't, something happens for a couple seconds and then stops, even if the boolean didn't change back.

I am yet to figure out a way to make something happen for a few seconds. The check is also happening constantly so as soon as the boolean changes, the movement starts and doesn't stop.

I might be sounding unclear so feel free to ask more specific questions...

EDIT: Cause a couple of people asked, I specifically need some of my game objects to move downwards for a couple seconds then stop and go back to moving horizontally (think Space Invaders).

Was it helpful?

Solution

You should look into properties, by using a public accessor which handles logic, and a private backing field.

public bool Enabled
{
    get {
        return enabled;
    }
    set {
        if (value != enabled)
        {
            //Do Something if it changes
        }
        enabled = value;
    }
}
private bool enabled;

The get code is executed when you get the property (Ex: bool isEnabled = Enabled), and the set is called when you set the property (Ex: Enabled = false) Note that the value is the new value being passed when you assign something to the property.

Now that you know how to check and execute code when the value changes, we can move on to the "something" you would like to do for a few seconds.

You can use a Timer found in System.Timers (Tutorial) to run code at an interval (And stop it after X intervals), or you can run code repeatedly using a StopWatch until it hits a certain time. Examples provided below,

Timer timer = new Timer(3000);
timer.Elapsed += (object sender, ElapsedEventArgs e) => { Console.WriteLine("Something"); };
timer.Enabled = true;

Stopwatch stopWatch = new Stopwatch();
stopWatch.Restart();
while (true)
{
    //If 3 seconds have elapsed, stop the operation
    if (stopWatch.ElapsedMilliseconds > 3000)
    {
        stopWatch.Stop();
        break;
    }
    //Do stuff repeatedly
    Console.WriteLine("Something");
}

All of that code can be put into a method and called from the set block. (And if you need to use the value of the property, you should pass it as an argument, otherwise you might run into issues if it changes)

EDIT: I did not see this was an XNA game, in that case, you need to set the time when you started moving, and keep moving until the current time is more than the start time, plus X amount:

Example:

Have a 3rd value called enabledChanged or whatever, that you can use later in your Update method so you know to start moving. Get the time when you start moving and set the changed flag back to false. Then keep moving until the current GameTime is past the desired time.

private bool enabledChanged;
private double startTime;
...
protected void Update(GameTime gameTime)
{
     if (enabledChanged) //If we should start moving
     {
          enabledChanged = false;
          startTime = gameTime.TotalGameTime.TotalSeconds;
     }
     if (gameTime.ElapsedGameTime.TotalSeconds <= startTime + 3) //If time is less than start time, plus 3 seconds, move
     {
          //Move object
     }
}

It really depends on what you want to do, but this is a general solution. (Because do you mean run something until time ends, completely stoping the operation? Or to repeat an action until time ends?)

OTHER TIPS

Well, you can save the last value of the boolean, then if it doesn't equal to the current value then set a new boolean value to true.

bool myBool = false;
bool lastValue = myBool;
bool doRunning = false;    

// Your loop
while (true) {
   .....
   if (myBool != lastValue) {
       doRunning = true;
       lastValue = myBool;
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top