1
\$\begingroup\$

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(); 
    }
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – 3Dave
    Jul 23, 2012 at 20:03

2 Answers 2

1
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ 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! \$\endgroup\$
    – 3D-kreativ
    Jul 22, 2012 at 15:01
  • \$\begingroup\$ 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? \$\endgroup\$
    – 3D-kreativ
    Jul 22, 2012 at 15:07
1
\$\begingroup\$

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;
    }
  }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .