Search code examples
actionscript-3flashconditional-statements2d-games

How can I create rules for my spawning platforms?


I'm working on a 2d vertical scrolling game that is based on doodle jump and i'm using flash and as3 to create it. I've put the scrolling and platform spawning and so far so good, but thing I randomize a x and y for each platform and obviously they just spawn wherever they feel like (inside the stage, that's my only actual rule). I wanna create rules so that max distance in between new platform and last one is, let's say 35px.

My current random code is:

public function createPlatform():void
        {
            //randomY();
            var newY:Number = Math.random() * 600;
            var X:Number = Math.random() * 500;
            var tempPlatform:mcPlatform = new mcPlatform();
            tempPlatform.x = X;
            tempPlatform.y = newY;
            platforms.push(tempPlatform);
            mcContent.addChild(tempPlatform);
        }

I also tried to do random just for Y this way:

private function randomY():void 
        {   
            var flag:Boolean = false;
            while (flag == false) 
            {
                newY = Math.random() * 600;
                if(newY < lastY && (lastY - newY) < 50 && (lastY - newY) > 10)
                    {
                        newY = lastY;
                        flag = true;
                    }
            }
        }

the point of the game is to have character jump from platform to platform and when the game scrolls its content it just spawns a new set of platforms.

P.S.: newY is declared in the beggining of the code as 600 so first one is always starting from stage height.


Solution

  • Instead of just randomly placing platforms, try starting at the bottom of the screen and increasing y by a random amount each time you place a platform.

    Something like:

    newY = Math.random() * 50;
    
    While (newY < 600) {
                var X:Number = Math.random() * 500;
                var tempPlatform:mcPlatform = new mcPlatform();
                tempPlatform.x = X;
                tempPlatform.y = newY;
                platforms.push(tempPlatform);
                mcContent.addChild(tempPlatform);
                newY += 35 + math.random() * 50;
            }