Hello Guest

Author Topic: Loading Animated Sprites while Walking etc  (Read 7053 times)

sfbaystudios

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Loading Animated Sprites while Walking etc
« on: December 25, 2012, 06:41:44 am »
Hello,

I'm a bit confused on how to actually use the animations created.  I've got animations for "Walk Left" and "Walk Right".  I have my character that can move.  But I'm not sure how (in Java, preferably) to bring the animations up instead of the static character, while the object is moving.

Any ideas?

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #1 on: December 26, 2012, 03:04:44 am »
Have you followed the animation walkthrough yet?
http://unikronsoftware.com/2dtoolkit/doc/

Its in C# but the concepts should be very very similar in JS. Drop me an email if you get stuck (support@unikronsoftware.com)

sfbaystudios

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #2 on: December 27, 2012, 02:33:53 am »
I have this code in Update() that somewhat works:


      if(Input.GetKey(KeyCode.RightArrow))
      {
         transform.position.x += moveSpeed;
      }
      if (Input.GetKeyDown (KeyCode.RightArrow))
      {
         CharacterWalkRight.renderer.enabled = true;
         renderer.enabled = false;
      }
      if (Input.GetKeyUp (KeyCode.RightArrow))
      {
         CharacterWalkRight.renderer.enabled = false;
         renderer.enabled = true;
      }

(The same code for "Left", of course).

When nothing is happening the character kind of bounces a pixel or too -- I think that's because the animations may go up/down a pixel or two rather than stay the same height.  Although they don't show without being enabled, they still exist and run.  Ultimately this is NOT the best solution, since all animations possible for the character are running non-stop.

Does anyone know a more legit solution to do this in java?

sfbaystudios

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #3 on: December 27, 2012, 06:24:45 am »
The answer has something to do with:


var CharacterWalkRight   : tk2dAnimatedSprite;

      if (Input.GetKeyDown (KeyCode.RightArrow))
      {
         CharacterWalkRight.renderer.enabled = true;
         CharacterWalkRight.GetComponent(tk2dAnimatedSprite).Play();      // THIS IS WHAT I WAS LOOKING FOR
         renderer.enabled = false;
      }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #4 on: December 27, 2012, 06:42:09 am »
Why are you enabling and disabling renderers? You don't need to do any of that.

CharacterWalkRight.GetComponent(tk2dAnimatedSprite).Play() is the same as CharacterWalkRight.Play(); they are both referring to the same object. All you need is

CharacterWalkRight.Play("..."); where ... is the name of the clip. You don't need multiple animated sprites, you just have one and play different clips on them.

sfbaystudios

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #5 on: December 27, 2012, 09:33:06 am »
CharacterWalkRight.Play();  kept throwing an error.  However, I see now that there are multiple Clips per animation -- I was making a new animation for each clip, so instead of 1 Animation with 4 Clips (idle/walk for left/right), I had 4 animations each with 1 clip.

Now I have 1 animation with 4 clips, and the following code is working, seems to be more right:


var CharacterAnimations   : tk2dAnimatedSprite;

function Start ()
{
   CharacterAnimations.Play("Idle Right");
}

function Update () {
   if (!didDie)
   {
      if(Input.GetKey(KeyCode.RightArrow))
      {
         transform.position.x += moveSpeed;
      }
      if (Input.GetKeyDown (KeyCode.RightArrow))
      {
         CharacterAnimations.Play("Walk Right");
      }
      if (Input.GetKeyUp (KeyCode.RightArrow))
      {
         CharacterAnimations.Play("Idle Right");
      }
        }
}

Does that look more correct? :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #6 on: December 27, 2012, 09:53:40 am »
That looks more correct now - ultimately you'll have to change how you select & play clips, but at you have a grasp on the concepts.

sfbaystudios

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #7 on: December 27, 2012, 10:04:43 am »
Oh shoot, I thought I was done :)

What do you mean I'll need to change how I select and play clips?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #8 on: December 27, 2012, 10:39:49 am »
Not yet, only when it becomes an issue. In some cases this may be more than enough.

sfbaystudios

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Loading Animated Sprites while Walking etc
« Reply #9 on: December 27, 2012, 07:27:08 pm »
Oh, ok :)  Cool.  Thanks!