How do I check if a sprite has a certain animation?

0 favourites
  • 10 posts
From the Asset Store
3 pixel charaters with over 8 animations each + animations with a gun
  • I have a family of sprites that have some shared animations, and also some unique animations.

    Is there a way to check if a sprite has a particular animation? (so that I can do something else if it doesn't?)

    There doesn't seem to be any sprite object conditions, but maybe there's something in JS or otherwise?

    (The current workaround is a web of variables to keep track of which sprite has which animations, which I must be doing wrong!)

    Thanks for any guidance!

    Tagged:

  • You can use JS to check if an animation exists:

    console.log(runtime.objects.Sprite.getFirstInstance().getAnimation("a3")!=null);

    Another method is to try to play the new animation, and then check if it has changed or not. But it's messy and may not be suitable in some situations.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks very much dop2000!

    Is there a way to use this code (or any JS) as a condition? I'm new to JS in Construct, and I can only find a way to use JS as an actions.

  • You can assign the result to a local or global variable. Then check that variable.

    localVars.varName=(runtime.objects.Sprite.getFirstInstance().getAnimation("a3")!=null);

    If the animation exists, varName should contain value 1. (but I haven't tried it)

  • Right! I've got it integrated into the event sheet, but I can't yet get it to find the animations. I'll keep at it, thank you!

  • That's because the JS code picks the first instance in the entire scope. (which will always be the same in the loop)

    Since you are using For Each loop, you need to pick the family instance by UID. Right before the script, save fSprite.UID to another local variable - varUID. Update the script - instead of getFirstInstance() use getInstanceByUid(varUID)

  • There's also

    getFirstPickedInstance()
    

    So you don't have to pick via UID. In a foreach the firstpicked should be the instance the loop is currently going over.

  • Thank you dop2000 and WackyToaster!

    I've tried both options here, but sadly neither are finding the animation.

    If you're interested, this is just the code I'm trying to use (both of your versions are here)

    drive.google.com/file/d/1uKcTWuydB0eFgoSZwgQ6uan-_foEcezp/view

    I think I've made a code mistake with getInstanceByUid(varUID), because it's returning errors in the console.

    This is likely all my error as I learn to use JS here, so if you see anything that could help, I'm all ears. Thank you again!

  • Turns out, you can't assign booleans to integers in JS. Try changing the code like this:

    localVars.debug=(runtime.objects.fSprite.getFirstPickedInstance().getAnimation("idle")==null?0:1);

    And the variable should be local. For global variables use:

    runtime.globalVars.varName

  • It worked out!

    FWIW, I've also made a feature request for a non-JS version of this, in case any other non-coders would benefit from it.

    github.com/Scirra/Construct-feature-requests/issues/234

    Thank you both for the help and the JS crash course!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)