Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your update code, you're checking a single time for a random value to be less than randomChance, and when it is, you spawn 4 apples.</p> <p>If you want them to spawn at different times, you would call them one at a time. I would make a function to create a randomly placed apple, and call that whenever your if statement is satisfied.</p> <p>Eg:</p> <pre><code>private function update(evt:Event){ if(Math.random() &lt; randomValue){ createApple(); } </code></pre> <p>Where createApple() would create a new apple, position it, add it to the array, and add it to the stage.</p> <p>If you're trying to create a different apple each time, you can put the different types in an array and iterate through it as you create new apples.</p> <p>EG.</p> <pre><code>private function createApple(){ ... var newApple = appleToSpawn[currentApple] ... } </code></pre> <p>Where appleToSpawn is an array of apple objects, and currentApple is an index of which apple you're currently on. </p> <p>Alternatively, you could do a series of comparisons to a random number, and spawn a different apple depending on which condition is satisfied.</p> <p>As for making sure they don't overlap, you can keep a history of their spawn points, and change how you get their random X value. Just iterate through the array of previous X values, and make sure the new one isn't within (oldX + (width/2)).</p> <p>Now for spawning more than 4 as you get more points, just change your randomChance to a bigger number as you go. If it's a bigger number, you'll satisfy your conditional statement more often, therefor spawning more apples. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload