Video Transcript
Now, let us see if we can take advantage of our init object to do something a little bit more than just trace here. I will take that trace statement out because that can be kind of obnoxious and let us set it up so that it can actually move our bubble. Now, to address the properties of the bubble, I will simply use a "this statement." Now in writing it, this applies to our init bubble. But remember, our bubble is going to inherit all of this features. We will setup this ._X and we will just increment that by let us say one for right now. I will do a plus equal, remember, that is our operator that will increment the variable by whatever value we choose and if I put one here at the end, will increase our X coordinate by one. Let us setup the Y coordinate as well, I will just copy and paste that, change our X to Y and we will be incrementing both variables and let us test our movie out with this simple movement.
Now, we should be able to create new buttons which also move. The only bad thing now is they are all going to move the exact same way. So, we got everything just kind of gradually going diagonally down the screen. Well, we got movement and all we really need to do is add a little randomness to it. Let us go back and set that up. I will close my preview window and what I would like to is give it some random value instead of that one sitting there.
Now, we are going to have to do a little bit of shuffling because first of all if I create my random variable here in your init bubble, all of my bubbles will inherit that same random variable which will not be very random anymore then. So, I am going to go down to where we actually make a bubble and I am going to create my random variable there. That way, each time, just like we are doing with the X and Y locations, we will get a different number. Now, there is one other complication that is, I want to make a random variable a new property of the bubbles. But ones again, I cannot make it a property of a bubble unless it already exists.
So, here is what I will do, I will go into the top of our function and I will attach this new variable that we are going to create back to our init, So, let us see how that is going to work. I will start off with init bubble and I will just make a new property for it by typing dot and our new property name, I think I will just call it speed. Speed X and we will do another one for speed Y, so, we can have two different variables controlling how this bubble is going to be moving in both the X and Y coordinate directions.
Now, I am going to set it equal to a random number, kind of like we did before. So, I just copied one of the statements down here and paste it up in here on the top and let us think of what kind of random number what we want. We use one in our test which is a little slow moving for a bubble, so, I would like it to be maybe a number between one and five. Now, I would also like to be able to move in either positive or negative direction. So, let us see how to set that up with our math random function.
Now, our math random will not give us a value that is negative. It will only give us a positive zero through one. So, if I wanted a number between minus five and plus five, I would multiply a random value by ten and then subtract five from it. So, you can see how that ranging is going to work. I would like a speed for the X direction and a separate one for the Y directions. We do not have to use the same for both. So, I am just going to copy that statement, make another variable down below but we will make it init bubble speed Y.
Now, you can see what we are doing. We are attaching that new property back to the init object which will be inherited down here when we make the duplicate movie clip. I need to assign that speed back up at the top and because we attached it to the init bubble object, I can simply say this.speed x and this.speed y, let us give that a try.
Now, when I make a new bubble, I am also generating a random X and Y coordinate. So, each one of my bubbles should probably pop off in a new direction, we got random pla
Now, let us see if we can take advantage of our init object to do something a little bit more than just trace here. I will take that trace statement out because that can be kind of obnoxious and let us set it up so that it can actually move...
click to read more