Hello Guest

Author Topic: Randomize between clips in one animated sprite in javascript  (Read 7441 times)

chenklein

  • Newbie
  • *
  • Posts: 6
    • View Profile
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

midasmax

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Randomize between clips in one animated sprite in javascript
« Reply #1 on: July 11, 2013, 02:38:31 pm »
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!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Randomize between clips in one animated sprite in javascript
« Reply #2 on: July 12, 2013, 12:10:47 pm »
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

chenklein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Randomize between clips in one animated sprite in javascript
« Reply #3 on: July 12, 2013, 03:33:59 pm »
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?? :-\

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Randomize between clips in one animated sprite in javascript
« Reply #4 on: July 12, 2013, 04:10:42 pm »
Did you attach it to a sprite animator? Thats what getcomponent does, it gets a component on the same gameobject...

chenklein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Randomize between clips in one animated sprite in javascript
« Reply #5 on: July 12, 2013, 09:31:45 pm »
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.

midasmax

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Randomize between clips in one animated sprite in javascript
« Reply #6 on: July 12, 2013, 10:02:18 pm »
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.

chenklein

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Randomize between clips in one animated sprite in javascript
« Reply #7 on: July 14, 2013, 11:13:36 am »
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