Okay, so let's consider. What we've established. So far in terms of the flow of a program
We know we have two new blocks of code that we've established
one is called set up and
one is called draw.
we write code inside both of these blocks between the curly brackets and
the code here in setup happens once and only once
First at the beginning and the code here and draw happens forever. It loops over and over again
when ever it gets to the end
It loops right back at the beginning stuff happens here once looping over and over again. So this is a very basic
simple
model for controlling the flow of
a program how it executes the instructions in what order
So in this video what I want to do is introduce
one more layer on top of this this idea of an event now truth be told a
Lot of programs don't have this animation loop at all everything is just a bunch of events. Right, this event triggers this event which triggers
this event, then the program waits until this event happens. That is certainly on a model for building
programs. A lot of web applications certainly work that way, but here we have an animation program
that's always running continuously, but some events might interrupt this loop.
So we finished through draw, an event execute we go back into draw. Two
obvious events that we can start with that are very simple to begin with are
this idea of an interaction event a mouse click or a key pressed. So for example
let's just say we want an event to be triggered when the mouse is pressed.
We can now write
Another block of code another function definition this time. It's called mousePressed, right. It's setup()
we have draw(), now we're adding mousePressed.
We put our parentheses we put our open curly bracket and our close curly bracket. This is an event.
This we can consider an event and it also only happens once. Now
I don't mean once in the course of the whole program,
but it executes once at the moment of the event. setup() truly happens only once when the program first begins.
mousePressed() might never occur we could write the most amazing sophisticated beautiful
code ever written our entire life put it here and if no one clicks the mouse it will never happen.
So this is something we have to realize this only happens when the mouse is pressed.
So the syntax remains the same as these blocks of codes, the difference.. than these blocks of code... these blocks of code
But the difference is the name of this method that we're implementing this block of code is called mousePressed().
Ok, so let's go over and take a look at implementing this in our actual program.
Here if you recall we have this continuous line drawing application where I'm just moving the mouse
and it's drawing a line from previous mouse position to current mouse location. So what if I... add
this event
mousePressed(), now one thing we should notice again is you've got to make sure you spell things correctly
If I wrote it as mousepressed with a lowercase p...
This is incorrect
Notice here that it doesn't turn blue, right?
It doesn't turn blue because it's not a known word without that capitalization does matter however
If I were to run this I get no error that's because I just want to bring this up now
It's technically going we're going to see later
How you can make up all sorts of other blocks of code that you want right now
we're trying to use specific ones, but so Processing thinks
I'm just trying to make up a new one called mousepressed with lowercase p
but it won't function correctly.
We want to make sure we have that capital p and we can see once we have that capital p
we've got the nice blue color. It's been recognized. So what's something I could put inside mousepressed()? I
don't know why don't we erase the background, we clear the background. Clear the background set a background of greyscale value 50 again. Here
I'm moving the mouse around every time I click it. It
erases what was there before?
So we can see here is... boom an event, execute that code go back and to draw boom an event execute that code go back
into draw(). So
This is really powerful. This is um...
we have so few tools right now because we haven't gotten to some of the really fundamental pieces of
defining our own variables and
conditionals to change the course of how the information flows in a program.
We just have these little pieces
But you can start to see that when you you could it can have you can have different things happen.
We could also do something kind of Kooky for example. I
could say
void keyPressed(), just to add another event and I could say background
you know, this I said do something kooky, and I'm like yeah, this is really crazy
Totally crazy. We're doing a different color when keyPressed().
But what can you do um so, we could say here every time I click the mouse
it erases the background with grey. When I... when I click the mouse
It's very hard for you to see that when I when I press a key then it does a green background so you can see
How this these are separate events a key pressed a mouse press. We're going to see later
how do you do something when a specific key is pressed? There's also a mouse
Released event right when the mouse is pressed [an] event is triggered when it's released another event is triggered
There's a mouse drag event you can look through the Processing reference
and you'll see there are bunch of built-in events that Processing can handle for you by
implementing that block of code that function definition
and then writing the code that you want to execute when that event is triggered inside there, okay?
So this is a nice little short video looking at my list of things
I think this kind of covered what I meant to cover
so now we really have control over the whole flow of a program and
what I would suggest to you is in most of the I gave you the exercise previously but try to
animate stuff with what you have so far now you have some events to try to have different things happen of what happens
what can you do with these events if background is a draw not as much of but... You can try different
configurations of this come up with problems that you
that you think you could solve and in many of these you might not be able to solve because you're going to need the concepts
we're going to go over in the next set of videos. So in the next set of videos, right
what just to remind you one of the ways we introduced this idea of variation into
the draw loop was through this idea of a keyword, mouseX, I called it a built-in variable it stands for a
number, the... the x location of the actual mouse. What we're going to look at
the next video is how do we make up our own variables?
I mean, if you think about it, we don't want to always just move things according to the mouse
we want might want something to move autonomously we need to store its position and...
and manipulate that position according to some mathematical formula. We're going to start to see how to do that in the next set of videos.
Sure, I missed something. I'll think of it. I'll write it off my list and then later. I'll add a new video
That'll be numbered with these numbers at the end. It'll fill in some of these details
That's kind of my plan right now because I already have something. I want to go back and add to them the earlier videos, okay?
Goodbye, and I'll talk to you later. I don't know I get it better like like sign off. You know
Catchphrase, but I'll think of that.