How to spawn movieclips at random locations?

Subscribe to How to spawn movieclips at random locations? 14 posts

avatar for Shadow_Craver Shadow_Craver 344 posts
Flag Post

In Doodle Jump, or The Last Robot 2, for example, platforms are spawned randomly, but you notice that less platforms are spawned the higher you go. Usually, I make a movieclip and stick all of the platforms in there, but I wanna randomize where they spawn, anyone have any idea how I can do that?

 
avatar for Mond Mond 749 posts
Flag Post

Math.random()

 
avatar for RTL_Shadow RTL_Shadow 1023 posts
Flag Post
Originally posted by Mond:

Math.random()

To continue on this:
If you are using AS3 I believe Math.random() returns between 0 and 1. So you would do:

x = Math.random()*stage.stageWidth;

as for spawning on the Y- just spawn one every 50 or so pixels, and increase this number the higher you get.

 
avatar for Shadow_Craver Shadow_Craver 344 posts
Flag Post

Thanks for the replies. I’m using AS2 But that’s alright, I’ll look into Math.random ()

 
avatar for Aesica Aesica 951 posts
Flag Post
Originally posted by RTL_Shadow:
If you are using AS3 I believe Math.random() returns between 0 and 1.

Technically, it’s between 0 and 0.99999999~. While that might seem like nitpicking, it’s important to know this because something like Math.floor(Math.random() * 2); will only ever return 0 or 1. Never 2.

Anyway regarding random platform generation, you’ll want to fine-tune things a little more than simply throwing around Math.random(). What if a few of them happen to spawn near 0, 0, and the rest appear close to the maximum width/height of your playfield? You’ve just randomly generated an impossible jump scenario that will result in an instant game-over. This is the kind of RNG players will complain about, and rightfully so.

 
avatar for Draco18s Draco18s 6860 posts
Flag Post

Be aware of the difference between “random” and “procedurally generated.” You’re asking about the former, and need the latter.

 
avatar for truefire truefire 3011 posts
Flag Post

it’s important to know this because something like Math.floor(Math.random() * 2); will only ever return 0 or 1. Never 2.

I used to do Math.floor(Math.random() * 1.99999999) for the longest time.

 
avatar for EndlessSporadic EndlessSporadic 10478 posts
Flag Post

I’m used to the C# syntax of Math.Random(lowerBoundInclusive, upperBoundExclusive) instead of this multiplication thing. C# will spoil you with syntax simplicity >.>

 
avatar for truefire truefire 3011 posts
Flag Post
inline public function rand(lowerBoundInclusive:Float,upperBoundExclusive:Float):Float
{
    return Math.random()*(upperBoundExclusive-lowerBoundInclusive) + lowerBouldInclusive;
}

It’s magic. Of course, the C# version, being native code* and not having Adobe involved, will be faster.

*Actually, I don’t know if C# runs natively. I’m assuming, since it has “C” in it.

 
avatar for feartehstickman feartehstickman 521 posts
Flag Post

0<= Math.random() <1

Multiply Math.random() by the range of values that you want and add on the lowest value that you want.

 
avatar for truefire truefire 3011 posts
Flag Post

So then 0 > 1?

You got the wrong comparison there.

0 <= Math.random() < 1

 
avatar for Shadow_Craver Shadow_Craver 344 posts
Flag Post

Well I did some research on procedurally generated levels. And it’s more complicated than I expected. I have to Give the AI information of the physics in order for it to make limits and avoid impossible jumps. Lol never done anything like this.

 
avatar for Draco18s Draco18s 6860 posts
Flag Post
Originally posted by Shadow_Craver:

Well I did some research on procedurally generated levels. And it’s more complicated than I expected. I have to Give the AI information of the physics in order for it to make limits and avoid impossible jumps. Lol never done anything like this.

Actually, all you should need to know is:

1) How high is the maximum jump height
2) How far left/right you can go in that time
3) Maximum jump distance on a perfectly horizontal jump

Place a platform within those distances from where the player starts (if it’s “the ground” then the X location is irrelevant, only the Y, conforming to known value 1). Then from that platform place another one within the confines of the above rules. Repeat.

If you have a “end location” then you need to bias your random numbers slightly so that on average the platforms tend towards that location

Add a few more random platforms (Idealy, pick another platform already created and place a new one within the same bounds as normal).

If you want to place a platform below another platform, then 1 & 2 come into play so that the already-created platform is within the distances to make a return jump.

 
avatar for feartehstickman feartehstickman 521 posts
Flag Post
Originally posted by truefire:

So then 0 > 1?

You got the wrong comparison there.

0 <= Math.random() < 1

facepalm
I trust you got the idea though.