Ok, so my question is very specific. I'm attempting to randomly generate a game stage for a 2D platformer I'm making. I want the platforms to be a set distance apart and I want the number of platforms created to be configureable. I believe the best way to do this is with a while loop that has an if statement nested in it, I've attempted it but can't get it working. Here's what I've tried.
var numLedges = 10;
var lastLedge;
var xSpace = randomIntFromInterval(0, 600);
var ySpace = randomIntFromInterval(0, 400);
var currentLedgeX = xSpace;
var currentLedgeY = ySpace;
var lastLedgeX = -1000;
var lastLedgeY = -1000;
var i = 0;
var loopCount = 0;
var maxLoops = 100;
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
function createLedges() {
do { // This is supposed to make sure the platforms are being created 100px away in any direction
if (currentLedgeX > lastLedgeX + 100 || currentLedgeX < lastLedgeX - 100 && currentLedgeY > lastLedgeY + 100 || currentLedgeY < lastLedgeY - 100) {
// Creates a ledge using phaser that is roughly 40 x 40px
var currentLedge = stillLedge.create(currentLedgeX, currentLedgeY, 'platform');
currentLedge.body.immovable = true;
lastLedgeX = currentLedgeX;
lastLedgeY = currentLedgeY;
i++;
}
loopCount++;
} while (i != numLedges || loopCount < maxLoops)
}
This doesn't appear to loop or it turns into an infinite loop somewhat randomly. If someone can think of a better way of doing this or a way to make this work I'd appreciate it. I'm out of ideas and haven't been able to find anything useful on the subject.
Aucun commentaire:
Enregistrer un commentaire