Video Transcript
Now to wrap everything all up, I would like to just hook up both of this buttons to something else in the program. Convenient thing would be these text fields sitting over here and we will wrap a variable while we are at it.
Since maybe hooking up both buttons, I am going to need one function for the red button and I am going to need one for the yellow one as well. So, I am going to copy the red button function. Copy that and I will paste it right down below. I will change it from the red button to the yellow and I will, of course change the trace. So we know which one of the users picked.
Now while we are at it, let us create a new variable called "the score". Now, I am going to create this variable before the buttons so that we have access to it. So, I will just go right down here underneath while I am working with George. I will add a comment and we will say, "Create the score variable".
Now remember, we are going to need is our bar keyword because we are using best practices. I can name the variable anything I want as long as it is not reserved words. So, I am just going to try typing, score here and I even point out. I am looking at the color. If it stays black that means it is not one of the reserved for it. So, I am pretty safe to use it. This is going to be a number, so I will type it as a number and I will initialized it by setting it equal to zero at the very beginning.
Now, here is what I am going to do. I am going to set it up so when you press the red button, score goes up and when you press the yellow button, the score goes down. I will just go inside the function and modify that score variable that we set up. So whenever you press the red button, I am going to take the original score variable and I am going to set it equal to score plus one. Now, that will take whatever the last value of the score variable was it start at zero and add one to it.
Now the first time you click the red button, the score will be zero plus one and it will be now equal to one. But the second time you press it, it will be two and so on. Now, I am going to do a very similar statement down here in the yellow button, except I will use minus one. Now, I am almost ready to try out this little script that we have created but we do not really have any way of finding out what the score is while the playhead is running. So, each time I change the score, I am going to trace it.
Now, by putting a trace inside the button function it is going to trace everytime you press the button. All of a sudden trace is seems like they are going to be a little bit more useful than they were before. They are not simply a static thing telling me what an item is equal to but I guess I find out what is equal to in an exact time when the program is running.
I will add another trace in the yellow button, tracing out the same message and now let us test our program out. I will hit "Ctrl+Enter" or Command Enter on the Mac. Now, when I click on the red button, it says "This is the red button" and it showing me the current score. I took the zero value and added one to it. If I press it again, it is now equal to two and again, it is now equal three.
Now, remember these two buttons are both manipulating the same variable. So if I press this one, it is taking the original score value of three and drop it down to two and if I go to the red button again and it will go up and so forth. Alright, so you can see how all of these things are going to work together to actually create a lot of the applications for us.
As a final a little element, let us take the score value and let us dump it into our score text field. I will close these both up again. Now, let us take a look at how we changed the text field before. I have got score variable and remember we use the text property and we just typed in whatever we would like it to be.
Now this line here, not only access our text property but it actually change the text in the in the field. Here is what I would like to do, I am going to copy this line out because we are going to use it again. So, I am just using "Ctrl+C" or "Command+C
Now to wrap everything all up, I would like to just hook up both of this buttons to something else in the program. Convenient thing would be these text fields sitting over here and we will wrap a variable while we are at it.
Since maybe hooking up both buttons, I...
click to read more