> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/graphics/animation/flatredball-graphics-ianimationchainanimatable/justcycled.md).

# JustCycled

### Introduction

The JustCycled property is a property which is usually false, but will be set to true when an animation either finishes or cycles. This can be used to perform logic whenever an AnimationChain ends. By default, objects (such as Sprites) which play animations will automatically loop. When this happens, the object begins displaying the first frame in the animation. When this occurs, the JustCycled property will be true. Keep in mind that when this value is set to true, the object will display the first frame of animation that frame. It is possible to prevent this by performing logic (such as destroying the object) when this is true.

### Code Example

The JustCycled property reports whether an object that is displaying an AnimationChain has just reached the end of an animation and has cycled to the beginning. You can perform logic in response this being set to true. The following code assumes MySprite is a valid Sprite. The if statement will trigger when the Sprite completes.

```
// assume MySprite is a valid Sprite that is animating
if(MySprite.JustCycled)
{
   // code goes here.
}
```

### Using JustCycled to play an animation just one time

JustCycled can be used to play an animation once, then stop the animation when it's cycled. For this example we'll use an animating Sprite called MySprite:

```
if(MySprite.JustCycled)
{
   MySprite.Animate = false;
   // The sprite animation will have cycled back to the first frame
   // To keep it at the last frame:
   MySprite.CurrentFrameIndex = MySprite.CurrentChain.Count - 1;
}
```

### JustCycled does not change after setting a new AnimationChain

If you are changing which animation an object is showing when JustCycled is set to true, keep in mind that JustCycled will stay true for the entire frame, regardless of whether you change chains in code. For example:

```
// This code checks to see if it's time to play the PerformPunch animation
// by testing if the Sprite has just finished cycling...
if(sprite.JustCycled && sprite.CurrentChainName == "StartPunch")
{
    // ... if so, then the PerformPunch animation is set...
    sprite.CurrentChainName = "PerformPunch";
    // ... however, JustCycled is still true at this point...
}
// ... which means this if-statement will always evaluate to true immediately
if(sprite.JustCycled && sprite.CurrentChainName == "PerformPunch")
{
    // ... and the Sprite will immediately go into the Idle animation
    sprite.CurrentChainName = "Idle";
}
```

### JustCycled and CurrentFrameIndex

When JustCycled is true and if the displayed object has its Cycling set to true, then CurrentFrameIndex will be 0 (unless an animation is playing backwards). In a typical setup (in an unmodified FlatRedBall template), the FlatRedBall Engine will perform its logic before custom game logic is executed every frame. This means that if JustCycled is true, custom code will have an opportunity to react to the cycle before the animation visually starts over. For example, consider this code:

```
if(SpriteInstance.JustCycled)
{
   SpriteInstance.Visible = false;
}
<pre>

The code above will hide the Sprite after it has cycled, so the first frame of the animation will not show again.
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/graphics/animation/flatredball-graphics-ianimationchainanimatable/justcycled.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
