githubEdit

TimedActivity

Introduction

The TimedActivity method is a method used by PositionedObjects (and objects which inherit from PositionedObject such as Spritearrow-up-right) to perform common every-frame logic. The TimedActivity method performs the following:

TimedActivity is automatically called for you on any PositionedObject that is part of a manager, so you will normally not need to call this method manually.

TimedActivity Signature

The TimedActivity signature is as follows:

public override void TimedActivity(float secondDifference, double secondDifferenceSquaredDividedByTwo, float secondsPassedLastFrame)

The arguments are used as follows:

When to manually call TimedActivity

TimedActivity is called automatically by the engine so you will not need to call it in most cases. However, there are some cases where you may need to:

  • If you have a PositionedObject which has either been removed from the engine or made manually updated, you may need to call this method manually if you would logic mentioned above to be applied (like Velocity).

  • If you would like to simulate more time passing in a single frame. The first argument indicates the amount of time that you would like applied in TimedActivity.

  • If you are performing unit tests and need to perform logic that would normally be performed by the engine on PositionedObjects.

Manually calling TimedActivity

TimedActivity may need to be manually called if you would like to simulate more time being passed in your frame. For example, you may be working on a game and you want to "fast-forward" time by 5 seconds to see where an object will end up. The TimedActivity method can accomplish this. The following code will move a PositionedObject forward in time by 5 seconds:

TimedActivity and Drag

The example shown above will result in perfectly accurate positioning if Dragarrow-up-right is 0. The reason for this is because Dragarrow-up-right is a linear approximation. Normally this linear approximation is sufficiently accurate; however, large values for secondDifference can result in inaccurate and even confusing behavior. It is even possible, given a large enough secondDifference, for Dragarrow-up-right to reverse the velocity of an object.

If you are using Dragarrow-up-right and want to manually call TimedActivity, you should either:

  1. Manually apply Dragarrow-up-right (see the Dragarrow-up-right page for information on how to do this)

  2. Call TimedActivity in a loop rather than all at once, as shown below.

Calling TimedActivity in a loop

Calling TimedActivity in a loop simulates the way that FlatRedBall calls TimedActivity once per frame. This can make Dragarrow-up-right behave properly. The following code shows how to call TimedActivity, simulating a game that runs at 30 frames per second:

Of course calling this method multiple times (150 in the example above) takes much more processing time than simply calling the method once with a larger secondDifference.

TimedActivity in objects inheriting PositionedObject

If you have a class which is inheriting from PositionedObject, then you may want to override the TimedActivity method. Doing this will allow you to write custom code which will be run automatically every frame if your object is added to the SpriteManager. In a Glue environment, code added to TimedActivity will be called prior to the custom code for a given frame.

Since this code is called by the SpriteManager during the FlatRedBall.Update call, your code may execute before or after TimedActivity for other engine-managed objects. In other words, if you require that other FlatRedBall types have finished applying properties such as velocity and acceleration for a given frame before your code is applied, you should not use TimedActivity for custom logic.

The following code is an example of what TimedActivity might look like in a class that inherits from PositionedObject:

Last updated

Was this helpful?