Time Based Movement in CocosSharp

PDF for offline use

Let us know how you feel about this

Translation Quality


0/250

How to move an object independent of frame rate

Recipe

Moving game objects should use time when calculating how far to move each frame so that behavior is consistent regardless of frame rate.

Movement can be handled through a Schedule call, which expects an Action<float>. The float parameter is the number of seconds since last frame, so it can be used to perform time based movement. First Schedule must be called:

// assuming this is called in an object that inherits from CCNode, such as an entity:
this.Schedule(PerformMovement);

PerformMovement implements the movement logic as follows:

void PerformMovement(float timeInSeconds)
{
    float velocity = 10; // in pixels per second
    this.PositionX += velocity * timeInSeconds;
}

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.