Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I thought this was a great concept; a method that is activated from the constructor when the object is created and in the method, see code below, after 3 seconds call another object to remove it from a list, but unfortunately this isn't working the way I want! There seems to be some problems with the timer, because the call to remove the object is done immediately without any delay! Have I missed something or isn't this possible? Perhaps in another way?

public void ExplosionTimer(GameTime gameTime)
    {
        seconds += (float)gameTime.ElapsedGameTime.TotalSeconds;
        if (seconds > 3)
            objectManager.ExplosionControl(); 
    }
share|improve this question
    
Why not just handle this in the object's Update()? Seems pointless to add another thread and have to worry about sync, etc when the functionality is already there. –  David Lively Jul 23 '12 at 20:03

2 Answers 2

up vote 1 down vote accepted

What does the method activated from the constructor do? Allow the gameobject to be explodable? Are you setting seconds = 0 in the constructor? What does objectManager.ExplosionControl() do? Loop over all objects and find the ones with seconds > 3? If so, why don't you just pass the object itself and ask it to be removed from the list?

objectManager.removeExplodedObject( this ); // for example

What you are describing should work. Are you calling ExplosionTimer on each update?

share|improve this answer
    
Thanks! Each object have there own update method and when I made a call to the ExplosionTimer from the update, then it worked like I wanted! –  3D-kreativ Jul 22 '12 at 15:01
    
I'm just a little bit confused how the GameTime object works!? Does the seconds variable start to count the first time the ExplosionTimer is called? –  3D-kreativ Jul 22 '12 at 15:07

Not exactly an answer to you problem but simply an alternative to your timer logic. You could also define a field that stores the time in the future when you want to trigger your behaviour.

The following example will cause damage & trigger a particle effect every half second regardles if this method is called more often than that period.

  private double _particleTimeSpan;

  public override void OnHurt(Vector2 location, float amount, DamageTypeEnum type, Actor cause, GameTime gameTime)
  {
    if (_particleTimeSpan < gameTime.ElapsedGameTime.TotalSeconds)
    {
        _hurtParticle.Trigger(location);
         Health -= amount;
        _particleTimeSpan = gameTime.ElapsedGameTime.TotalSeconds + 0.5;
    }
  }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.