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

Random Movement Logic

Lets make our button when clicked move to a random position on the stage. In this video we will see how we can interact again with the stage and the Math.random()

Video Transcript

00:00for our x. Now our x is exactly like a variable so we're going to set inside of it a value
00:05and we want to put there a number that will tell it what value we want it to be.
00:09Now if we just run our application for a second I'm just going to put it at zero for a second
00:14and if we run our application we're going to see this so as soon as I'm going to click on it
00:18it's going to snap to the side. Now that's not exactly what we want to do but we want to take
00:22the width of the of the stage which we know how to get there already but we want to subtract
00:27also the width of our movie clip because we want to make sure that if we hit the zero zero
00:32that it won't hit it it won't take that into account so you'll move it around only based on
00:38our coordinate of our button which is a good pointer that we didn't really talk about this
00:41but if we go back into our movie clip by default so we know that our zero zero is up here of our
00:47button this is our point our our pivot point for our movie for our button excuse me so we want to
00:53we know that we know that we want to ignore all the width of this item because we just want to
00:58move it around minus that size and to do that is quite easy so I'm first going to approach my stage
01:06and I'm going to ask for my stage width and then I'm going to subtract out of it my btn move
01:14width now I'm going to grab this whole thing and put it in a round bracket because I want to
01:20duplicate it by something between zero and one to give us a random value to do that we have our
01:24math class which has a function which is called random open close bracket same thing as we do
01:32with our trace only this time around we're not sending anything and there we go we got our x
01:39roaming around randomly let's do the same thing also to the y before we actually publish it
01:44so I'm just going to copy this whole not delete it but copy it and paste it in and all we really
01:50have to change is we want to change it from x to y and from stage width we want to stage it change
01:55it to stage height and from width we want to change it to height and notice how I always delete
02:01also the dot because it's just easier then you don't have to type as much you could always just
02:05use those hints that you're getting and every version of flash obviously has better hints so
02:10if you're with cs5 the hints are really really nice and and easy or if you're working with
02:15flex then obviously flex or flash builder has fantastic code hints all right so if we run our
02:24application now we should be getting our button moving around in our state so if I click it it's
02:28going to go to a random position if I click it it's going to go to a random position again
02:32and so on and so forth but our button number two will not do anything

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!