• Runtimes
  • Removing Attachment

Related Discussions
...

I have an animation that makes the player raise an umbrella to fall slower through the sky, but if the animation is interrupted i.e. I set a new animation, the player is still holding the umbrella. I want to be able to remove the umbrella and cancel the "umbrella raise" animation if the animation is interrupted. I have tried setting the attachment to null and it doesn't seem to work. I do not use the editor and really don't understand how it works...I am just the programmer and have read that if there are keys set during the animation that I would need to set it to null every frame but this doesn't seem like a good solution. Anyway some one can help me wrap my head around this? I am using the libgdx runtime. Also I set every animation to track 0 in case this information is needed.

I believe it has something to do with the animation mixing because if I don't automatically set umbrella fall after umbrella raise AND umbrella raise is finished then the umbrella is removed, but if the animation isn't finished yet or umbrella fall is active (it loops so never finishes) then the umbrella is not removed.

Here I try to remove the umbrella when any animation is started other than either of the umbrella animations. When "umbrella raise" is finished it starts "umbrella fall" animation which loops. Umbrella raise is just the player raising the umbrella toward the sky and umbrella fall is just the player body wiggling through the sky.

animState.addListener(new AnimationStateAdapter() {
         public void start (int trackIndex) {
            if (animState.getCurrent(trackIndex) != null && !animState.getCurrent(trackIndex).getAnimation().getName().matches("Umbrella Fall")
                  && !animState.getCurrent(trackIndex).getAnimation().getName().matches("Umbrella Raise")) {
               skeleton.findSlot("Umbrella").setAttachment(null);
               System.out.println("Umbrella removed");
            }
         }
      });

Well I did some digging around and print the attachment after setting it to null and it indeed is set right back to the umbrella (I believe because it is keyed to be attached at frame 0). Ultimately What I did to fix the issue is remove the key in the spine editor and set the attachment when setting the animation during runtime on top of keeping the above code to remove the attachment if the animation is interrupted. Call me crazy, but I feel this is very counter-intuitive and there should be a much simpler way to just remove the attachment or clear the animation by name during runtime. I feel I must be missing something as I love the spine animation tool but this is a big turnoff. I now have to tell the animator to not actually show the attachments when he makes the animation as I have to set them during runtime 🙁 I would love to get some feedback and see if there is a much better approach to the issue. Thanks in advance!

When you apply an animation that keys an attachment to be visible, it's no surprise the attachment is made visible. However, when you change from one animation to another, no state from the previous animation should be left behind (unless you used AnimationState clearTrack or AnimationState clearTracks to abruptly cease applying an animation). Do you see the behavior (umbrella from previous animation stays shown in subsequent animations) in the Preview view inside Spine, or the Skeleton Viewer when changing animations? Are you using multiple tracks?

If you are still having trouble, please create a simple example. The easiest way to do that is run Skeleton Viewer from source, load your skeleton, close Skeleton Viewer, uncomment these lines, keep the first line and change the second line to play the animation you want. Add more lines to queue up more animations. Next post or email those lines and include your skeleton JSON file so we can see the problem.

If you want to make sure an attachment is hidden, no matter what is keyed in an animation, hide the attachment after applying the animation:

animState.apply(skeleton);
skeleton.findSlot("Umbrella").setAttachment(null);

However, as mentioned, this probably isn't needed.

This is probably an extremely newbie question, but how do I run from source? I downloaded the project from github and am having issues after importing the project into eclipse with the build path....ughh software can be so frustrating lol.

Easiest is to just copy the spine-libgdx source into your libgdx project's source folder. Otherwise, the spine-libgdx project in git depends on the libgdx projects. You can remove those projects and instead depend on the libgdx JARs.