Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Created June 16, 2012 13:33
Show Gist options
  • Save robotlolita/2941344 to your computer and use it in GitHub Desktop.
Save robotlolita/2941344 to your computer and use it in GitHub Desktop.
Get a random Y position for a new enemy being spawned, which is NOT "crowded" (no other active enemy must have the same rough Y position).
function getYPositionNotCrowded() {
var radius = 8
var y = 0
var blocking = false
do {
y = randIntRange(height, canvas.height - height)
for (var i = 0; i < enemies.length; ++i)
if (enemies[i].isActive)
blocking = y > enemies[i].y && y < (enemies[i].y + height)
} while (blocking)
return y
}
/*
// Get a random Y position for a new enemy being spawned, which is NOT "crowded" (no other active enemy must have the same rough Y position).
function getYPositionNotCrowded()
{
var theHeight = 8;
var theY = 0;
var numberOfEnemiesWhoAreNotBlocking = 0;
var numberOfActiveEnemies = 0;
while (1)
{
theY = randIntRange(theHeight, canvas.height - theHeight);
numberOfEnemiesWhoAreNotBlocking = 0;
numberOfActiveEnemies = 0;
for (var i = 0; i < enemies.length; i++)
{
if (enemies[i].isActive)
{
numberOfActiveEnemies++;
if (!(theY > enemies[i].y && theY < (enemies[i].y + theHeight)))
numberOfEnemiesWhoAreNotBlocking++;
}
}
if (numberOfEnemiesWhoAreNotBlocking == numberOfActiveEnemies)
break;
}
return theY;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment