2D Toolkit Forum

2D Toolkit => Support => Topic started by: kliqingk on May 24, 2013, 09:29:56 am

Title: How can I change the anim in a tk2dAnimatedSprite?
Post by: kliqingk on May 24, 2013, 09:29:56 am
Hey:
    I created a tk2dAnimatedSprite with the function Instantiate.  How can I change the anim in the instance?

    public tk2dAnimatedSprite prefab;
    tk2dAnimatedSprite temp = Instantiate(prefab, new Vector3(0,0,0), Quaternion.identity) as tk2dAnimatedSprite;

    Then I want to use another tk2dSpriteAnimation instead of the default. How to do it?
Title: Re: How can I change the anim in a tk2dAnimatedSprite?
Post by: unikronsoftware on May 24, 2013, 04:31:59 pm
temp.anim = newAnim; // load using resources.load, etc.

in tk2d 2.0 - we've changed it to be a bit more sensible.
temp.Library = newAnimLibrary; // again load in using resources.load.

You can also play clips without setting the library -
temp.Play( newAnim.GetClipByName("clipname") );

 
Title: Re: How can I change the anim in a tk2dAnimatedSprite?
Post by: kliqingk on May 27, 2013, 03:23:33 am
How to using resources.load?

temp.anim = Resources.Load("SpriteAnimationName", typeof(tk2dSpriteAnimation));

There is an error : Cannot implicityly convert type 'UnityEngine.Object' to 'tk2dSpriteAnimation'.
Title: Re: How can I change the anim in a tk2dAnimatedSprite?
Post by: unikronsoftware on May 27, 2013, 11:38:28 am
Resources.Load returns a UnityEngine.Object, simply cast to tk2dSpriteAnimation.

temp.anim = Resources.Load("SpriteAnimationName", typeof(tk2dSpriteAnimation)) as tk2dSpriteAnimation;
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

Don't forget that the sprite animation must be in a resources folder for this to work.