Skip to content

Commit 2f11cd6

Browse files
authored
Add an expression to get the number of frames in the current animation of a sprite object (#4722)
1 parent d9de2a3 commit 2f11cd6

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Core/GDCore/Extensions/Builtin/SpriteExtension/SpriteExtension.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,14 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsSpriteExtension(
583583

584584
obj.AddExpression("Sprite",
585585
_("Image"),
586-
_("Animation frame of the object"),
586+
_("Current frame of the animation of the object"),
587+
_("Animations and images"),
588+
"res/actions/sprite.png")
589+
.AddParameter("object", _("Object"), "Sprite");
590+
591+
obj.AddExpression("AnimationFrameCount",
592+
_("Number of frames"),
593+
_("Number of frames in the current animation of the object"),
587594
_("Animations and images"),
588595
"res/actions/sprite.png")
589596
.AddParameter("object", _("Object"), "Sprite");

GDJS/GDJS/Extensions/Builtin/SpriteExtension.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ SpriteExtension::SpriteExtension() {
108108
spriteExpressions["Animation"].SetFunctionName("getAnimation");
109109
spriteStrExpressions["AnimationName"].SetFunctionName("getAnimationName");
110110
spriteExpressions["Sprite"].SetFunctionName("getAnimationFrame");
111+
spriteExpressions["AnimationFrameCount"].SetFunctionName("getAnimationFrameCount");
111112
spriteExpressions["AnimationSpeedScale"].SetFunctionName(
112113
"getAnimationSpeedScale");
113114
spriteExpressions["ScaleX"].SetFunctionName("getScaleX");

GDJS/Runtime/spriteruntimeobject.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,17 @@ namespace gdjs {
754754
return this._currentFrame;
755755
}
756756

757+
getAnimationFrameCount(): number {
758+
if (this._currentAnimation >= this._animations.length) {
759+
return 0;
760+
}
761+
const currentAnimation = this._animations[this._currentAnimation];
762+
if (this._currentDirection >= currentAnimation.directions.length) {
763+
return 0;
764+
}
765+
return currentAnimation.directions[this._currentDirection].frames.length;
766+
}
767+
757768
/**
758769
* @deprecated
759770
* Return true if animation has ended.

0 commit comments

Comments
 (0)