Beginner: I'm following a Flappy Bird tutorial and attempting to have my bird player flap its wings continuously. The code in the tutorial video works, but I'm looking use my own code for the same functionality. I don't want to get in the habit of copy/pasting code when there are multiple solutions to solve the same problem.
The code from the video below:
private SpriteRenderer sr;
public Sprite[] sprites;
private int spriteIndex = 0;
private void start()
InvokeRepeating("AnimateSprite", .25f, .25f);
private void AnimateSprite()
{
spriteIndex++;
if (spriteIndex >= sprites.Length)
{
spriteIndex = 0;
}
sr.sprite = sprites[spriteIndex];
}
This works. However, when attempting to create my own solution, I can not get the sprite to alternate. My code also interferes with the gravity and jump function of my bird. Here is my code.
private SpriteRenderer sr;
public Sprite[] sprites;
private int spriteIndex = 0;
void update()
{
AnimateSprite();
}
private void AnimateSprite()
{
for (int i = 0; i < sprites.Length; i++)
{
sr.sprite = sprites[spriteIndex];
StartCoroutine(SpriteChangeTime(.25f));
spriteIndex++;
if (spriteIndex >= sprites.Length)
{
spriteIndex = 0;
AnimateSprite();
}
}
}
private IEnumerator SpriteChangeTime(float waitTime)
{
yield return new WaitForSeconds(waitTime);
}
My goal was to use a coroutine combined with a for loop. I also believe I made the function recursive.
Can someone explain why my code does not work? Is there a solution that includes some form of a timer or for loop?
I'd appreciate any input. Thank you, and please let me know how/if I should format my question differently to better follow stackoverflow etiquette and make it easier to read.