Now, let us what we can do about keeping the bubbles inside the frame. For that, all we need to do is make sure that they do not leave those X and Y coordinates we saw originally. I think this was about 65 and that was about 635, we are looking about 80 at the top and 375 at the bottom. So, let us just add into our init object where we checked the location. I will go back where we set the values and here is what I think we will do. We will check to see if the X location is outside of one of those two boundaries we were looking at. What we can do to fix that is simply take whatever the X speed is, and make it a negative. That way, it will be traveling at the same speed but traveling in the opposite direction. Now, I will do that by adding a couple of statements below, our X and Y coordinate assignments where we do a conditional, wherever we need to do if, then some kind of a condition and then we need to add some statements into the grouping of the conditional statement itself.
Our condition will be based on, let us do the X first. This._X, now, I am going to check, let us say the left side of the screen. That was a coordinate position of about 65. So, if the X value is less than 65, then we must have crossed that boundary. I will simply take that X speed, this speed X and I want to make it into the opposite, or a negative or whatever it is. I can do that pretty easily by multiplying it negative one. So, I will do a times, equals, minus one.
Now, I need to close off my “if condition grouping.” And if we want to test that one condition, I will have to keep pressing our bubble button until it moves off to the left side of the screen. I think I want to make it move a little bit faster just so we can do that. So, I am going to change the speed variable from ten to 20 and now, we are looking for a number between minus ten and ten; so, I will subtract ten from that. I will make that change them both in these and we will have a little bit faster bubbles.
Okay, let us try that out. Now, I make a new bubble. I am looking for one that is going towards the left boundary over here. And you can see, when it gets to the left boundary, you can see that it reverses its direction on the X and it goes off. So, there we got another one. So, all we need to do is have that same kind of check on our remaining three boundaries. Let us set that up. Now, I can just follow this with four If statements but I would also like to show some different ways to be able to write your conditions in the If statements. I have basically got two checks I am going to do on the X coordinate. One is for the left boundary and one is for the right boundary.
Now, basically, if it hits either boundary, I want to take the speed and multiply it by negative one. So, let us add an operator to our condition that allows us to check two conditions at ones. Basically, I have two operators I can use inside of an If condition statement. I have got an “and” and an “or.” Now, in “and” operator we will check two conditions and make sure both of them are satisfied. An “or” will let you put two conditions and let either one be satisfied.
You can do an “or” by using the pipe key. Now, on the PC that is shift and it is either above or below the Enter key depending on what kind of keyboard you have but it is a two little vertical lines. I am going to type them in and you will get something looks like that. That will give us an “or statement” and I can add second condition after it. I will do this ._x and this time, we are looking to see if it is greater than, so you see, our screen size was 700 pixels wide minus 65, that would be about 635.
Now, using the “or” we will get either this condition or this condition satisfied, it will give us the same speed change. Let us go ahead and set up the Y value while we are it. I think I can copy this and we can modify it. So, we will just copy and paste down below and we will be checking the Y coordinates this time in both cases. I will be changing the Y speed and this time, we are looking for the top of the screen. Our Y value would be less than, I
Transcription by:
Scribe4you Transcription Services