1-11 Putting It All Together!

Let’s apply everything we’ve learned so far to create some interesting animated effects!

A Toggle Button

You know how to create mouse roll-overs, but what about an interactive button that you can click with the mouse to toggle on or off?  Here’s a good first attempt to make a clickable button:

Not bad, but if you try this code you’ll see that it has an undesirable flickering effect.  The code behaves like the mouse is being clicked many times when it is only being clicked once.  I included the print() function on line 17 to show you every time buttonOn is changed.  The problem is that the draw() function is being called 60 times per second, so even the shortest mouse click will often register several times, toggling the buttonOn variable between false and true more than once.

The solution to this problem is to define a mousePressed() event handling function to detect mouse clicks, and then check if it’s within the bounds of the button.  This much more accurately detects each mouse click as a single event.

This code works perfectly, no more flickering.  You’ll notice a few other improvements to the code.  I have replaced lines 1116 in the old code with just one line:

buttonOn = !buttonOn

Recall that the ! (not) operator reverses a Boolean value.  This line will change the value of buttonOn to true if it is currently false, and vice versa.  No if statement is needed — very elegant!

Edge Detection

In lesson 1-7 we animated a circle from left to right across the canvas, but then it disappeared off the canvas.  Using what we’ve learned about system variables (in particular, width) and if statements we can now detect when the circle reaches the left or right side of the canvas and reverse its direction.

The new code is on lines 15 to 18.  Here we simply check if the center of the ellipse has gone beyond the right side of the canvas (width) or left-side (0).  The speed variable controls the number of pixels the circle moves in each frame, but it also controls the direction.  If speed is a positive integer, the circle will move left to right, and if it is a negative integer it will move right to left.  Changing an integer from positive to negative (or the reverse) is easily done by multiplying the value by -1 i.e., 2 * -1 = -2 and -2 * -1 = +2.

Gravity

When an object falls, the force of gravity causes it to accelerate (speed up) as it falls.  Let’s change the previous example, such that the circle falls from the top of the canvas and bounces up and down.

The circle starts by moving down (speed = 1).  In each frame, we add +1 to speed such that the circle appears to fall faster and faster.  Upon hitting the bottom of the canvas, speed is set to -20.  Since speed is always added to circleY, adding a negative value like this will cause the circle to move up.  On each frame, we continue to add +1 to speed, so eventually, -20 will become positive again and the circle will fall again.  This process repeats giving a bouncing effect.

Multiple Paths

Let’s move the circle around the canvas in a clockwise direction!

There are 4 paths (states) to this animation: (0) left to right, (1) top to bottom, (2) right to left, and (3) bottom to top.  We’ll use a variable called path to track what state we are in and adjust the direction of the circle accordingly.  Once the circle reaches the end-point of its current path, the path variable is changed to activate the next path in the animation.

You Try!

  1. Modify the Edge Detection example, such that the circle only starts moving if the user clicks on the circle with the mouse.  If they click on the circle while it’s moving, it stops again.
  2. Modify the Edge Detection example, such that the circle moves in a random x and y direction (use random values between -5 and 5).   You will need 2 variables to do this, speedX and speedY.  The circle should bounce off all four sides of the canvas.  The direction that the circle bounces should make sense in terms of physics.  For example, if the circle hits the top of the canvas, you should reverse the y-direction, but not the x-direction.  Start the circle at (200,100) on the canvas.  Do you find that sometimes the circle does not move?  Why is this?  Try to solve this problem!
  3. Modify your solution to Q2 so that you have 1 red and 1 green circle moving in random directions, and bouncing off the edges of the canvas.
  4. Extra Challenge: Create a simple interactive PacMan animation.  Start by drawing PacMan in the center of the canvas.  If the user presses the ‘a’, ‘s’, ‘w’, or ‘d’ keys have PacMan move in the appropriate direction.  Pacman should only move vertically or horizontally, not diagonally.  If PacMan exits the left or right side of the canvas he should reappear on the opposite side, similarly for exiting the top or button side of the canvas.
  5. Extra Challenge: Enhance Q4 such that PacMan is drawn facing in the direction he is travelling in.  Don’t forget to adjust his eye position!