1
Support / Walking Animation Then Idle Problems
« on: May 26, 2013, 04:13:35 pm »
I have the C# code that makes the Animation move (from scripting an animated sprite in the documentation). And I did some modifications to it so when I walk left (a-key) the animation will play and when you take your finger of the key it will go to idle. But I can't get the same thing to happen when I'm walking right (d-key). My right animation doesn't even play it stays at idle. I haven't found much on this so thanks for your time and help.
here is my code:
here is my code:
Code: [Select]
public class TutorialAnimController : MonoBehaviour {
// Link to the animated sprite
private tk2dAnimatedSprite anim;
// State variable to see if the character is walking.
private bool walking = false;
// Use this for initialization
void Start () {
// This script must be attached to the sprite to work.
anim = GetComponent<tk2dAnimatedSprite>();
}
// This is called once the hit animation has compelted playing
// It returns to playing whatever animation was active before hit
// was playing.
void HitCompleteDelegate(tk2dAnimatedSprite sprite, int clipId) {
if (walking) {
anim.Play("walkRight");
}
else {
anim.Play("idle");
}
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A)) {
// Only play the clip if it is not already playing.
// Calling play will restart the clip if it is already playing.
if (!anim.IsPlaying("walkLeft")) {
anim.Play("walkLeft");
// The delegate is used here to return to the previously
// playing clip after the "hit" animation is done playing.
anim.animationCompleteDelegate = HitCompleteDelegate;
}
} else {
if (!anim.IsPlaying("idle")) {
anim.Play("idle");
anim.animationCompleteDelegate = null;
walking = false;
}
}
if (Input.GetKey(KeyCode.D)) {
if (!anim.IsPlaying("walkRight")) {
// Walk is a looping animation
// A looping animation never completes...
anim.Play("walkRight");
// We dont have any reason for detecting when it completes
anim.animationCompleteDelegate = null;
walking = true;
}
}
}
}