Sign in to follow this  
SpaceJesus

Better random generation for spawning enemies in Flash game

Recommended Posts

SpaceJesus    105
I am not a full programmer, but I doing a little retro game in Flash AS2.

I having a problem with the flash Random. It´s not random enough. I use random to generate the position X where the enemies spawn and the type of enemy (between 5 types).

Random in Flash generate clustered values. Sometimes a lot of one type of enemy spawn. There are tricks I can use to get a more varied random results?

Ex (not the entire code):



[quote]

// random function between 5 types of enemies, with different frequence
this.onEnterFrame = function()
{
_root.pE = (Math.random()*1000);

if ( ((_root.pE >30) and (_root.pE <250)) or ((_root.pE >600) and (_root.pE <750)) or ((_root.pE >800) and (_root.pE <850)))
{
_root.qI="e01p";
}
if ( ((_root.pE >250) and (_root.pE <320)) or ((_root.pE >750) and (_root.pE <800)) )
{
_root.qI="e02p";
}
if ( ((_root.pE >320) and (_root.pE <400)) or ((_root.pE >850) and (_root.pE <920)) )
{
_root.qI="e03p";
}
if ( ((_root.pE >400) and (_root.pE <450)) or ((_root.pE >950) and (_root.pE <1000)) )
{
_root.qI="e04p";
}
if ( ((_root.pE >0) and (_root.pE <30)) or ((_root.pE >920) and (_root.pE <950)) )
{
_root.qI="e05p";
}


//control how much ships are generate each time
if(_root.iTotalShips <10)
{
_root.iEnemies++;
_root.attachMovie(_root.qI, "Enemy" + _root.iEnemies, _root.getNextHighestDepth());
_root["Enemy" + _root.iEnemies]._x = 20+random((_root.horizontalspace)-60);
_root["Enemy" + _root.iEnemies]._y = -30 - (random(400));
_root.iTotalShips++;
}

[/quote]

Link for the game (not finished):
[url="http://mcn78.sites.uol.com.br/Game1m.html"]http://mcn78.sites.uol.com.br/Game1m.html[/url]

Share this post


Link to post
Share on other sites
ApochPiQ    23000
The problem is likely not the random number generator itself, but the [i]way[/i] you use the random values from it. Note how you have several intervals that will cause enemy type 1 to spawn: (30, 250) and (600, 750) and (800, 850). It looks to me like you're trying to implement a weighted random (so certain types are more common than others) but this isn't the best way to do that.

A more mathematically sound result can be obtained by doing the following:

- Assign a percentage of "likeliness" to each enemy category
- Make sure all the percentages add up to 100%
- Generate a random number from 0 to 100
- If the number is less than the [i]least likely[/i] enemy type's percentage, use that type
- Subtract the least likely enemy type's percentage from the number
- If the number is less than the next enemy type's percentage, use that type
- Repeat until you have chosen an enemy

You can also eliminate the subtraction steps by testing for [0, 10) and [10, 20) and so on instead of always looking for 0-n where n is a probability number.


That should eliminate a lot of your clumping issues :-)

Share this post


Link to post
Share on other sites
SpaceJesus    105
Thks. I still need add sounds and work with the game difficult/balance/progression, but made a new version:
[url="http://mcn78.sites.uol.com.br/st/st.html"]http://mcn78.sites.u...m.br/st/st.html[/url]


I don´t know if I did the correct way to follow your [left][background=rgb(250, 251, 252)]mathematical logic:[/background][/left]

[img]http://img.photobucket.com/albums/v156/harlockmbb/code.gif[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this