- Змінено
Slot color change animation from code [libgdx]
Hello,
Is there any way to create animation for slot from a code? I created ship in Spine and I want to change color of one slot during runtime. I have a lot of slots and I don't want to make animation for each slot in editor. I would like to execute animation on selected slot from code.
I noticed that I can't edit data of Timeline and Animation instances. Constructors are very limited. Also there's missing setters.
Is it possible to make animation from code? Anyone knows how to do it?
It's probably easiest to modify a slot's color, eg slot.getColor().a = 0.5f
. You can change the value each frame over time to animate it. This is likely easiest for simple fading in or transitioning from one color to another.
You could use Spine's animation and timelines to do it, but I think that would only make sense if you have a long, defined sequence of color changes. All timelines have public constructors and a setFrame
method.
Works!
Thank you very much.
private boolean createBlinkAnimation(int slotIndex) {
if (slotIndex != -1 && slotIndex <= skeleton.getSlots().size) {
Animation.AlphaTimeline alphaTimeline = new Animation.AlphaTimeline(5, 0, slotIndex);
alphaTimeline.setFrame(0, 0, 1f);
alphaTimeline.setFrame(1, 0.5f, 0.3936f);
alphaTimeline.setFrame(2, 0.5f, 0);
alphaTimeline.setFrame(3, 0.5f, 0.3936f);
alphaTimeline.setFrame(4, 0.5f, 1); //Time calculation - Value from spine/30frames
Array<Animation.Timeline> timelinesOutput = new Array<>(1);
timelinesOutput.add(alphaTimeline);
Animation anim = new Animation("blink", timelinesOutput, 1.0333334f);
skeleton.getData().getAnimations().add(anim);
return true;
}
return false;
}
addAnimation(1, "blink", true, 1f);
Looks good! You could save one line of code using:
Array<Animation.Timeline> timelinesOutput = Array.with(alphaTimeline);
Note AlphaTimeline is a CurveTimeline, so has setStepped
and setBezier
if you want a curve different from the default of linear.