4.1 – Rollovers and States

Review

  • Blocks of code{}
  • Variables should be lower case!
  • r = constrain(r, 0, 255); –> check the Processing reference!
Download and review last week’s correct homework here

Rollovers

  • Check to see if the mouse is within the boundaries of a box
  • Check for a mousePress — Download the example here
  • If both of these things are true, toggle a boolean variable between true and false
  • Move this to void mousePressed to leave a switch turned on — Download the example here
  • button = !button; (If the boolean button is true, make it false. If the boolean button is false, make it true)

State

  • int state;
  • State increases every time the mouse is pressed –> state = state + 1;
  • We don’t want to state to increase forever though!
  • Test the size of state and if it gets larger than the number of states,  set the state back to 0
  • Test for different states in your draw loop
  • Download the state example here

Gravity

  • The bouncing ball sketch taught us that an object moves by altering its location according to speed.
  • location = location + speed;
  • speed = speed + gravity;
  • “dampening” effect –> reverse the speed by multiplying by .95 instead of one. This slows down the square each time it hits the floor, simulating real-world physics.
  • Download the gravity example here

 

Print this page

Leave a Reply

Your email address will not be published. Required fields are marked *