I use this code for animating UI Sprite (not Sprite2D):
[code]void UIManager::StopAnimation(Sprite* element)
{
element->SetAttributeAnimationTime(“Image Rect”, 0.0f);
element->SetAttributeAnimationSpeed(“Image Rect”, 0.0f);
}
void UIManager::StartAnimation(Sprite* element)
{
element->SetAttributeAnimationSpeed(“Image Rect”, 1.0f);
}
void UIManager::AddAnimation(Sprite* element, const char* textureName, int frameWidth, int frameHeight,
int totalNumFrames, int maxColumns, float delay)
{
element->SetTexture(CACHE->GetResource(textureName));
element->SetBlendMode(BLEND_ALPHA);
element->SetSize(frameWidth, frameHeight); // Sprite size on screen.
SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
float time = 0.0f;
for (int i = 0; i < totalNumFrames; i++)
{
int row = i / maxColumns;
int col = i % maxColumns;
IntRect rect(col * frameWidth, row * frameHeight, (col + 1) * frameWidth, (row + 1) * frameHeight);
animation->SetKeyFrame(time, rect);
time += delay;
}
// At the end again show firs frame
animation->SetKeyFrame(time, IntRect(0, 0, frameWidth, frameHeight));
animation->SetInterpolationMethod(IM_NONE);
element->SetAttributeAnimation("Image Rect", animation);
}
[/code]
Using:
auto animatedSprite = UI_ROOT->CreateChild<Sprite>();
AddAnimation(animatedSprite, "Textures/AnimExclam.png", 20, 64, 20, 10, 1.0f / 24);
I have not tried, but most likely this approach will work for Sprite2D.