2D Toolkit Forum
2D Toolkit => Support => Topic started by: chenklein on July 11, 2013, 01:21:00 pm
-
Hello :D
Im trying to make an array of the animations in one animated sprit so that I can randomize between them by name. Haw do I access these animations in JavaScript ?
Thanks
-
Hi there!
What you could do is get ahold of the tk2dSpriteAnimator object:
anim = GetComponent<tk2dSpriteAnimator>();
Next, you can access the array of clips in the animator in the clips field:
anim.clips
The script reference is here:
http://www.unikronsoftware.com/2dtoolkit/docs/2.10/html/classtk2d_sprite_animation.html
Hope this helps!
-
In JS, it'll be a bit like this:
var anim : tk2dSpriteAnimator = GetComponent(tk2dSpriteAnimator);
anim.Library.clips will be the list of clips. You can then pick a random one and call anim.Play( anim.Library.clips[ clipId ] ); where clipId is a random number between 0..clips.Length -1
-
hey
so - i did what u wrote me to do (without the rendomize part) just to get the list of clips , i've add this code to my animated sprite and it looks like this -
var anim : tk2dSpriteAnimator;
anim = GetComponent(tk2dSpriteAnimator);
var ListOfClips:tk2dSpriteAnimationClip[] = anim.Library.clips;
function Update ()
{
print (ListOfClips);
}
then i attached the same animated sprite to the variable anim in the inspector but i keep getting this arrow -
"NullReferenceException: Object reference not set to an instance of an object "
what am i doing wrong?? :-\
-
Did you attach it to a sprite animator? Thats what getcomponent does, it gets a component on the same gameobject...
-
Im sorry but i am not familiar with the new version of 2d toolkit and don't fully understand haw to use and code the sprite animator.
i keep getting this error - "NullReferenceException: Object reference not set to an instance of an object "
guess ill need to dig deeper in the 2D Toolkit Documentation.
-
i keep getting this error - "NullReferenceException: Object reference not set to an instance of an object "
Could you post the full output of that error message, especially the line number and the script which it's referring too. Also, if you could either copy the offending line, or the whole script? It would make it easier to nail down.
What I think is that you have not added the Tk2dSpriteAnimator script to whatever object you are trying to animate. Check your inspector to make sure you can see a Tk 2d Sprite Animator (Script) component on your Game Object.
-
OOk , finally got it!
this is my code - its attached to a sprite with animator and in the inspector in the "anim" variable i've attached the same sprite animator
var anim : tk2dSpriteAnimator;
var ListOfClips:tk2dSpriteAnimationClip[];
ListOfClips = anim.Library.clips;
function Start ()
{
PlayRandomAnimation () ;
}
function PlayRandomAnimation ()
{
var RandomClipNumber:int = Random.Range(0 , ListOfClips.Length);
anim.Play(ListOfClips[RandomClipNumber]);
}
thank you very much midasmax and unikronsoftware