Add an expression to get the number of frames in the current animation of a sprite object (#4722)

This commit is contained in:
D8H
2022-12-21 09:30:03 +01:00
committed by GitHub
parent d9de2a3177
commit 2f11cd667b
3 changed files with 20 additions and 1 deletions

View File

@@ -583,7 +583,14 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsSpriteExtension(
obj.AddExpression("Sprite",
_("Image"),
_("Animation frame of the object"),
_("Current frame of the animation of the object"),
_("Animations and images"),
"res/actions/sprite.png")
.AddParameter("object", _("Object"), "Sprite");
obj.AddExpression("AnimationFrameCount",
_("Number of frames"),
_("Number of frames in the current animation of the object"),
_("Animations and images"),
"res/actions/sprite.png")
.AddParameter("object", _("Object"), "Sprite");

View File

@@ -108,6 +108,7 @@ SpriteExtension::SpriteExtension() {
spriteExpressions["Animation"].SetFunctionName("getAnimation");
spriteStrExpressions["AnimationName"].SetFunctionName("getAnimationName");
spriteExpressions["Sprite"].SetFunctionName("getAnimationFrame");
spriteExpressions["AnimationFrameCount"].SetFunctionName("getAnimationFrameCount");
spriteExpressions["AnimationSpeedScale"].SetFunctionName(
"getAnimationSpeedScale");
spriteExpressions["ScaleX"].SetFunctionName("getScaleX");

View File

@@ -754,6 +754,17 @@ namespace gdjs {
return this._currentFrame;
}
getAnimationFrameCount(): number {
if (this._currentAnimation >= this._animations.length) {
return 0;
}
const currentAnimation = this._animations[this._currentAnimation];
if (this._currentDirection >= currentAnimation.directions.length) {
return 0;
}
return currentAnimation.directions[this._currentDirection].frames.length;
}
/**
* @deprecated
* Return true if animation has ended.