2D Toolkit Forum
2D Toolkit => Support => Topic started by: sfbaystudios 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!
-
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)
-
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?
-
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;
}
-
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.
-
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? :)
-
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.
-
Oh shoot, I thought I was done :)
What do you mean I'll need to change how I select and play clips?
-
Not yet, only when it becomes an issue. In some cases this may be more than enough.
-
Oh, ok :) Cool. Thanks!