Shadow_Craver
344 posts
|
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?
|
|
|
Mond
749 posts
|
|
|
|
RTL_Shadow
1023 posts
|
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.
|
|
|
Shadow_Craver
344 posts
|
Thanks for the replies. I’m using AS2 But that’s alright, I’ll look into Math.random ()
|
|
|
Aesica
951 posts
|
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.
|
|
|
Draco18s
6860 posts
|
Be aware of the difference between “random” and “procedurally generated.” You’re asking about the former, and need the latter.
|
|
|
truefire
3011 posts
|
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.
|
|
|
EndlessSporadic
10478 posts
|
I’m used to the C# syntax of Math.Random(lowerBoundInclusive, upperBoundExclusive) instead of this multiplication thing. C# will spoil you with syntax simplicity >.>
|
|
|
truefire
3011 posts
|
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.
|
|
|
feartehstickman
521 posts
|
0<= Math.random() <1
Multiply Math.random() by the range of values that you want and add on the lowest value that you want.
|
|
|
truefire
3011 posts
|
So then 0 > 1?
You got the wrong comparison there.
0 <= Math.random() < 1
|
|
|
Shadow_Craver
344 posts
|
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.
|
|
|
Draco18s
6860 posts
|
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.
|
|
|
feartehstickman
521 posts
|
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.
|