Search code examples
c#.netxnamonogame

Monogame - How do I make my if statement loop?


The title of this question may sound stupid, but please read the entire question first, and yes, I'm aware of all types of loops and I'm able to use the perfectly fine when programming.

I'm building a 2D game, and when my player collides with a spike, I want another character to slowly walk (It's quite hard to explain, however that doesn't matter), but here's the code for it:

 if (player.Bounds.Intersects(anneTrigger.Bounds))
 {
      Anne.UpdateForAnne(gameTime);  
      Anne.LoadHumanContent(Content);
 }

Briefly explaining the code, the condition checks if the player has collided with the anneTrigger spike, then it calls Anne.UpdateForAnne(gameTime) and Anne.LoadHumanContent(Content), which animates the character and makes the character walk, which is what I want, and works perfectly fine.

What's the issue?

The problem is, this only works whilst the player is on the spike, once my player walks past and off the spike, the other character stops walking, which is not what I want. I need it to continue as long as the player has collided with the spike already.

I have tried to use a loop instead of an if statement, but that just crashes my game. I'm sure I may have to use a boolean however I'm not too sure how, I have tried previously however it did not work.


Solution

  • Set a bool variable in the class outside of your update method.

    private bool AnneActivated = false;
    
    if (player.Bounds.Intersects(anneTrigger.Bounds))
    {
       AnneActivated = true;
    }
    
    if(AnneActivated)
    {
       Anne.UpdateForAnne(gameTime);  
       Anne.LoadHumanContent(Content);
    }