- Home /
Run Animation with Movement SpriteManager2
Hi,
I'm not sure if this is the best place to ask this, but I'm hoping someone has done something similar before. I'm using SM2 and I want to enable the run animation when the arrows are hit. The problem is, in my implementation it actually always draws the static texture when it's moving, then starts the run when an arrow is released. Perhaps someone has some code examples of doing user input with SM2. Thanks in advance.
var moveSpeed : float;
function Update () {
var packedSprite : PackedSprite = gameObject.GetComponent("PackedSprite");
if ( (Input.GetKeyDown(KeyCode.RightArrow)) || (Input.GetKeyDown(KeyCode.LeftArrow)) ) {
packedSprite.PlayAnim("Run");
transform.Translate(Input.GetAxisRaw("Horizontal") * Time.deltaTime * moveSpeed, 0, 0);
} else {
packedSprite.RevertToStatic();
}
}
Answer by Brady · Apr 28, 2011 at 07:40 PM
First, you want to make sure you cache the PackedSprite reference earlier in your code, like in Start(), rather than doing it each frame, which will be slow.
Next, use Input.GetKey() instead, which will return the status of that key every frame, whereas GetKeyDown() only returns true in the frame that the key was pressed.
Finally, instead of using PlayAnim(), use DoAnim(). PlayAnim() re-starts the animation every time it is called, so once you've switched to GetKey(), it will be getting called for every frame that the key is held down. Which means PlayAnim() would constantly restart the animation on the first frame every frame, making it look like it isn't moving. DoAnim(), by contrast, only starts the animation if it isn't already running.