2D Toolkit Forum

2D Toolkit => Support => Topic started by: Artifact on February 16, 2012, 06:52:05 pm

Title: Animating multiple sprites with the same script
Post by: Artifact on February 16, 2012, 06:52:05 pm
Hello, I need some help animating 2 sprites with the same script. Right now I have 2 enemy sprites I wan't to animate with the same script. The method that moves and animates them looks like this:

Code: [Select]
    void MoveToPlayer()
    {
        if (enemyTransform.localPosition.x >= player.transform.localPosition.x)
        {
            enemySpeed = 5;
            enemyTransform.Translate(Vector3.left * amountToMove, Space.World);
            anim.Play("WalkLeft");

        }
        else if (enemyTransform.localPosition.x <= player.transform.localPosition.x)
        {
            enemySpeed = 5;
            enemyTransform.Translate(Vector3.right * amountToMove, Space.World);
            anim.Play("WalkRight");
           
        }
    }

The problem is that if I add this script to both my sprites it only animates one of them, the other just moves without any animation. Some help with this issue would really be appreciated.

Thanks in advance :)

Title: Re: Animating multiple sprites with the same script
Post by: unikronsoftware on February 16, 2012, 09:27:23 pm
It looks like you're only animating one of the sprites (anim.Play()). You will need to call anim.Play(...) on the other sprite too.
Title: Re: Animating multiple sprites with the same script
Post by: Artifact on February 17, 2012, 12:19:25 am
I solved the issue, my tk2dAnimatedSprite was static, thats why it only reacted on one of my sprites....
Title: Re: Animating multiple sprites with the same script
Post by: unikronsoftware on February 17, 2012, 06:55:58 am
Ah that makes sense - no way to tell from the snippet you posted though! Glad you've resolved the issue.