02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Randomize

Next step we want to randomize our new 100 elements we added to the stage. We visit the properties scaleX, scaleY, x, y of our newly created movieclip.

Video Transcript

00:00Next step, we want to randomize everything, so let's do that quite easy. We've done it before already.
00:05Just going to go to our stage width, and we're going to duplicate it by our math random,
00:11which would return a number between zero and one, randomly duplicated by the stage width,
00:16which would position our item in a random position on the stage.
00:20We want to do the same thing for the y, so I'm just going to copy that line and change it to y
00:25and change our stage width to dot stage height, and our math random is good.
00:32So now if we just run our application now, we should get a little bit more depth in our application.
00:37So, well not depth yet, we get a bit of randomization, which is very nice.
00:42Now one problem that we're having is that all of our items are the exact same size,
00:48which kind of breaks a little bit of an illusion of something from coming up to down.
00:53So you want to, even though we're working in 2D, let's just put a little bit of randomization
00:57in the size of the elements to make it look like some are further away while some are closer.
01:04So we're going to put a randomization, a value of let's say between half of its original size
01:12to two times its original size.
01:15So our range is going to be between 0.5 to 2, or, well, in ActionScript 3 that would be that way,
01:25because there's a property called, there's a property for movie clips and display objects in general.
01:32So mzOvel, and we have here a little property that's called xScale, or Scale x in ActionScript 3,
01:41and we want to do the same, we're going to do equal to the scale y,
01:46because we want to put the same value in both of them, which is a valid way of writing it.
01:50And here we're going to put that value, so first of all we want to give it 0.5 no matter what,
01:54plus 1.5, but we don't really want to do 1.5, we want to have a random value of 1.5 in Math Random, right?
02:01So we're going to get a value of 1.5 between 0 and 1.5, plus that was 0.5, which would always come back.
02:09So if we run it just one more time, we're going to see that we're going to get a little bit more details,
02:13and these details are going to help this illusion of some sort of depth going on.
02:17Obviously it's a very basic depth, but that's all we need for now.
02:21Let's move on to the next step where we're going to start animating our elements.
02:26So let's start off by first moving them all in the same exact speed,
02:30which is obviously not going to look very authentic.
02:33Now we already have a little problem that we're not really keeping a reference to our movie clips,
02:38and if you look at line 11, you'll see what I mean, because as soon as I create that new variable,
02:43and I add it to the stage, I override it with a new one each time.
02:47Now technically, just to make things look right,
02:50we probably would want to take this variable definition and put it outside of the loop,
02:55because there's no reason to redefine it each time.
02:58We could put a new value inside of our variable each time,
03:00and this is completely legit and even more recommended,
03:03because it's going to cut down the amount of processing that Flash needs to do.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!