[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:02.75,0:00:03.88,Default,,0000,0000,0000,,Whoo hoo! Another animation. Dialogue: 0,0:00:03.88,0:00:06.38,Default,,0000,0000,0000,,This time we've got a ball\Nmoving across the screen, Dialogue: 0,0:00:06.38,0:00:09.50,Default,,0000,0000,0000,,and you guys know how this is done. Dialogue: 0,0:00:09.50,0:00:11.13,Default,,0000,0000,0000,,We've got a variable, "x,"\Nthat tells us the position of the ball, Dialogue: 0,0:00:11.13,0:00:14.12,Default,,0000,0000,0000,,a variable "speed" that tells us\Nhow far the ball moves every time, Dialogue: 0,0:00:14.12,0:00:15.12,Default,,0000,0000,0000,,and a familiar draw loop Dialogue: 0,0:00:15.13,0:00:17.42,Default,,0000,0000,0000,,where we're redrawing\Nthe background every time Dialogue: 0,0:00:17.42,0:00:23.21,Default,,0000,0000,0000,,setting fill colors and drawing\Nthe ellipse at position x, Dialogue: 0,0:00:23.21,0:00:24.21,Default,,0000,0000,0000,,and every single time, Dialogue: 0,0:00:24.21,0:00:26.18,Default,,0000,0000,0000,,we're going to change "x"\Nto be the old value "x," Dialogue: 0,0:00:26.18,0:00:29.53,Default,,0000,0000,0000,,plus the variable "speed." Dialogue: 0,0:00:29.54,0:00:33.88,Default,,0000,0000,0000,,So if I make "speed" smaller,\Nyou can see it moves slower. Dialogue: 0,0:00:33.88,0:00:39.04,Default,,0000,0000,0000,,I can make it negative,\Nso we move backwords, Dialogue: 0,0:00:39.04,0:00:39.05,Default,,0000,0000,0000,,or I can make it zero,\Nand the ball would stop moving. Dialogue: 0,0:00:39.05,0:00:40.46,Default,,0000,0000,0000,,But as long as speed is not zero, Dialogue: 0,0:00:40.50,0:00:43.83,Default,,0000,0000,0000,,eventually the ball\Nis going to go off the screen. Dialogue: 0,0:00:43.83,0:00:46.20,Default,,0000,0000,0000,,And I can always press\Nthe restart button to bring it back, Dialogue: 0,0:00:46.20,0:00:48.17,Default,,0000,0000,0000,,but you know, after awhile,\Nthat gets old. Dialogue: 0,0:00:48.36,0:00:52.70,Default,,0000,0000,0000,,You restart, and restart... Dialogue: 0,0:00:52.71,0:00:55.75,Default,,0000,0000,0000,,So, how about this? When the ball reaches\Nthe right edge of the screen, Dialogue: 0,0:00:55.75,0:00:58.78,Default,,0000,0000,0000,,instead of going off the edge\Nof the screen like it's doing now, Dialogue: 0,0:00:58.78,0:01:00.50,Default,,0000,0000,0000,,I want it to turn around. Dialogue: 0,0:01:00.50,0:01:04.52,Default,,0000,0000,0000,,And, I know how to turn the ball around,\NI can just say, "speed" gets negative 5. Dialogue: 0,0:01:04.52,0:01:10.34,Default,,0000,0000,0000,,If I make the speed negative,\Nthen the ball would go backwards. Dialogue: 0,0:01:10.45,0:01:12.86,Default,,0000,0000,0000,,But here is the problem. Dialogue: 0,0:01:12.88,0:01:18.25,Default,,0000,0000,0000,,I only want to change the speed\Nif the ball has reached the right edge. Dialogue: 0,0:01:18.25,0:01:20.75,Default,,0000,0000,0000,,Hmmm... so I think I already said it. Dialogue: 0,0:01:20.75,0:01:25.92,Default,,0000,0000,0000,,I only want to change the speed,\Nif the ball has reached the right edge. Dialogue: 0,0:01:25.92,0:01:29.62,Default,,0000,0000,0000,,I think this calls for an "if" statement. Dialogue: 0,0:01:29.63,0:01:31.21,Default,,0000,0000,0000,,Up until now, we've only been giving Dialogue: 0,0:01:31.21,0:01:32.98,Default,,0000,0000,0000,,the computer commands\Nto run no matter what. Dialogue: 0,0:01:32.98,0:01:34.58,Default,,0000,0000,0000,,If statements are a way to say, Dialogue: 0,0:01:34.59,0:01:37.08,Default,,0000,0000,0000,,"Hey dude, I want you to run this code, Dialogue: 0,0:01:37.09,0:01:39.54,Default,,0000,0000,0000,,but only under these\Nspecific circumstances." Dialogue: 0,0:01:39.54,0:01:42.54,Default,,0000,0000,0000,,"So only change the speed\Nif the ball has reached the right edge." Dialogue: 0,0:01:42.54,0:01:45.86,Default,,0000,0000,0000,,And here's how it looks in code. Dialogue: 0,0:01:45.88,0:01:47.21,Default,,0000,0000,0000,,All you do is type "IF" Dialogue: 0,0:01:47.21,0:01:50.96,Default,,0000,0000,0000,,and then a pair of parentheses,\Nand then a pair of brackets. Dialogue: 0,0:01:50.96,0:01:54.33,Default,,0000,0000,0000,,Inside the parentheses\Nwe're going to put the condition. Dialogue: 0,0:01:54.34,0:01:57.08,Default,,0000,0000,0000,,Inside the brackets\Nwe're going to put the code to run. Dialogue: 0,0:01:57.09,0:02:00.08,Default,,0000,0000,0000,,So the way it works is,\N"If this condition is true, Dialogue: 0,0:02:00.08,0:02:03.00,Default,,0000,0000,0000,,then run this code;\Notherwise, don't bother." Dialogue: 0,0:02:03.00,0:02:06.54,Default,,0000,0000,0000,,So in our case, the condition\Nis the ball reaching the right edge. Dialogue: 0,0:02:06.54,0:02:10.37,Default,,0000,0000,0000,,How do we know if the ball\Nhas reached the right edge? Dialogue: 0,0:02:10.38,0:02:16.42,Default,,0000,0000,0000,,We've got this variable "x"\Nthat tells us where the ball is, Dialogue: 0,0:02:16.42,0:02:22.96,Default,,0000,0000,0000,,and I know that the edge of the canvas\Nx position 400, so let's see. Dialogue: 0,0:02:22.96,0:02:25.08,Default,,0000,0000,0000,,When "x" is greater than 400, Dialogue: 0,0:02:25.08,0:02:30.46,Default,,0000,0000,0000,,then we know that the ball has gone\Npast the right edge a little bit. Dialogue: 0,0:02:30.46,0:02:33.04,Default,,0000,0000,0000,,So let's see how that works. Dialogue: 0,0:02:33.04,0:02:38.98,Default,,0000,0000,0000,,And the code to run, we already said\Nbefore, we're just going to change speed. Dialogue: 0,0:02:38.99,0:02:45.29,Default,,0000,0000,0000,,Speed gets negative five. We're going\Nto press restart and see what happens. Dialogue: 0,0:02:45.29,0:02:48.14,Default,,0000,0000,0000,,So this time, when the ball reaches\Nthe right edge, it bounces! Yaaaay! Dialogue: 0,0:02:48.16,0:02:50.54,Default,,0000,0000,0000,,And then it keeps going off the screen. Dialogue: 0,0:02:50.54,0:02:51.62,Default,,0000,0000,0000,,But that's OK, because we can keep doing\Nthe same thing on the other side. Dialogue: 0,0:02:51.67,0:02:52.67,Default,,0000,0000,0000,,So this time, we want to check\Nif the ball has reached the left edge. Dialogue: 0,0:02:52.67,0:02:57.96,Default,,0000,0000,0000,,And that's when x is less than zero,\Nand what we want to do Dialogue: 0,0:02:57.96,0:03:03.04,Default,,0000,0000,0000,,is make speed positive again,\Nso speed gets 5. Dialogue: 0,0:03:03.04,0:03:08.08,Default,,0000,0000,0000,,Alright, and then we're going\Nto press restart, and this time... Dialogue: 0,0:03:08.12,0:03:11.71,Default,,0000,0000,0000,,boing... Dialogue: 0,0:03:11.71,0:03:14.42,Default,,0000,0000,0000,,boing... boing... Dialogue: 0,0:03:14.50,0:03:16.67,Default,,0000,0000,0000,,yay! It works. Dialogue: 0,0:03:16.67,0:03:18.92,Default,,0000,0000,0000,,And I know we're checking to see\Nif the ball has gone past the edges Dialogue: 0,0:03:18.96,0:03:21.42,Default,,0000,0000,0000,,but it feels like it's going\Na little too far past the edges. Dialogue: 0,0:03:21.63,0:03:23.67,Default,,0000,0000,0000,,And if you remember,\Nthese two parameters control Dialogue: 0,0:03:23.67,0:03:25.34,Default,,0000,0000,0000,,where the center of the ellipse is drawn. Dialogue: 0,0:03:25.54,0:03:31.50,Default,,0000,0000,0000,,So right now, by the time\Nthe center reaches the edge, Dialogue: 0,0:03:31.50,0:03:36.92,Default,,0000,0000,0000,,half the ellipse has already gone\Npast the edge. Dialogue: 0,0:03:36.92,0:03:42.08,Default,,0000,0000,0000,,So if we want to fix that,\Nwe just stop the ellipse a little sooner. Dialogue: 0,0:03:42.08,0:03:47.33,Default,,0000,0000,0000,,So if our edge is here, at 400 and we want\Nto stop our ellipse when it gets here, Dialogue: 0,0:03:47.33,0:03:53.12,Default,,0000,0000,0000,,and we can see from the function\Ncall that the ellipse has width 50, Dialogue: 0,0:03:53.12,0:03:58.63,Default,,0000,0000,0000,,so that means from the center\Nto the edge, that's going to be 25. Dialogue: 0,0:03:58.63,0:04:04.62,Default,,0000,0000,0000,,So we want to stop it\Nwhen the center reaches 375, Dialogue: 0,0:04:04.63,0:04:06.88,Default,,0000,0000,0000,,that's 400 minus 25. Dialogue: 0,0:04:06.88,0:04:08.100,Default,,0000,0000,0000,,So instead of checking\Nfor x greater than 400, Dialogue: 0,0:04:08.100,0:04:10.97,Default,,0000,0000,0000,,we're going to check for\Nx greater than 375. Dialogue: 0,0:04:10.97,0:04:13.29,Default,,0000,0000,0000,,Instead of checking for x less than 0. Dialogue: 0,0:04:13.29,0:04:15.54,Default,,0000,0000,0000,,I'll check for x less than 25. Dialogue: 0,0:04:15.58,0:04:19.58,Default,,0000,0000,0000,,And now it's perfect! Yay!\NLook at at that ball bounce.