Search code examples
androidanimationrandomgame-loop

How to "randomize" animation in main game loop


I have an animation(see video link below) that is apart of an app I'm working on. I basically have fish swimming across the screen. Currently I have an array of about 20 fish each initially positioned one behind the other, and then "swim" across the screen in order. My problem is, this is not the behavior I want. This is just a temporary workaround. What I need to happen instead is that after each fish has reached a certain threshold(let's say less than the last 1/5 of the canvas width), another fish from the array is chosen at random, positioned, and then swims across.

This should repeat for the entire life of the activity. I tried generating a random number once each fish reaches that point, then using it to position the fish at that index and then floating it across but an obvious issue with that is if the random number that is chosen corresponds to the index of a fish that is ALREADY on the canvas..it will disappear(so that it can be repositioned again, etc). Aside from that issue, from my understanding of game dev. so far(I'm still very much a beginner)..random number generation inside the main game loop should be avoided at all costs. But I'm just not sure of any other way to go about this. Hoping to get some insight on an approach from more experienced devs. Thanks guys. Here's a link to the YouTube video I posted for this question

https://www.youtube.com/watch?v=QM6t6f9TaUQ&feature=youtube_gdata_player

TL;DR - I want a bunch of fish to swim across the scream in random order, and have this process repeat until the user exits the activity/app.


Solution

  • If it's ok if the spacing between the fishes remains constant for the life of the app, simply generate the randomish inter-fish offsets at application startup. 1st first swims in... when it reaches random offset #1, fish #2 enters. When fish #2 reaches random offset #2, fish #3 enters, and so on. When a fish leaves the other side of the screen, it goes back in the queue and waits to enter.