1
00:00:08,620 --> 00:00:13,620
In this second lecture in Module Eight, we
continue using mouse input.

2
00:00:13,620 --> 00:00:16,116
And before we talk about the details,
here's

3
00:00:16,116 --> 00:00:19,179
a reminder of the learning objectives for
this module.

4
00:00:21,990 --> 00:00:25,110
So last time, we learned how to use the
mouse location to move a

5
00:00:25,110 --> 00:00:27,750
character arund in the window, including
clamping

6
00:00:27,750 --> 00:00:30,600
that character so it stays in the window.

7
00:00:30,600 --> 00:00:34,510
This time, we're going to learn how to use
a mouse button

8
00:00:34,510 --> 00:00:40,190
for input to change the character to some
other random character.

9
00:00:40,190 --> 00:00:42,140
So let's go to the project.

10
00:00:43,410 --> 00:00:46,470
Sorry.
Let's not go to the project quite yet.

11
00:00:46,470 --> 00:00:47,220
Let's have

12
00:00:47,220 --> 00:00:52,170
you do an in lecture quiz first based on
your recollection of the things that are

13
00:00:52,170 --> 00:00:57,365
contained in the mouse state object that
gets returned when we call get-state.

14
00:01:01,060 --> 00:01:03,509
Now lets go to the project, and we are

15
00:01:03,509 --> 00:01:07,810
going to just be starting from where we
were before.

16
00:01:07,810 --> 00:01:11,750
And so we've got a character, I'll run it
again so we can see how it works.

17
00:01:11,750 --> 00:01:14,940
We've got a character that follows the
mouse around the screen,

18
00:01:14,940 --> 00:01:18,269
staying clamped in the screen so they
can't go outside the bounds.

19
00:01:19,460 --> 00:01:22,661
Let's actually talk for a minute or two

20
00:01:22,661 --> 00:01:26,347
through the documentation, to see how we
can

21
00:01:26,347 --> 00:01:28,080
use a mouse button.

22
00:01:29,180 --> 00:01:32,568
So I'm going to search on mouseState
again, just like

23
00:01:32,568 --> 00:01:35,725
I did last time, although perhaps I'll
spell it

24
00:01:35,725 --> 00:01:38,497
correctly so that we actually get a hit,
so

25
00:01:38,497 --> 00:01:42,560
we have these mouse state members as we
saw before.

26
00:01:42,560 --> 00:01:46,210
Five different buttons is what XNA
provides for us.

27
00:01:46,210 --> 00:01:49,660
And some other stuff too, that location
that we used last time.

28
00:01:49,660 --> 00:01:51,770
So let's look at the left button.

29
00:01:51,770 --> 00:01:56,210
Because this description says it returns
the state of the left mouse button.

30
00:01:56,210 --> 00:02:01,450
And when we look at that we see that it
returns something called button state.

31
00:02:01,450 --> 00:02:04,420
Now button state is an enumeration.

32
00:02:05,430 --> 00:02:07,080
And an enumeration.

33
00:02:07,080 --> 00:02:10,388
Now lets go look at button state so we can
see how it really works.

34
00:02:10,388 --> 00:02:11,798
[SOUND]

35
00:02:11,798 --> 00:02:17,842
And enumer.
And enumeration

36
00:02:17,842 --> 00:02:23,840
is a new data type that only has specific
values.

37
00:02:23,840 --> 00:02:30,240
So, if we have button state, which is what
gets returned from left button,

38
00:02:30,240 --> 00:02:36,790
the left button property of the mouse
state, then that returned value.

39
00:02:36,790 --> 00:02:39,760
Can only be one of two different values.

40
00:02:39,760 --> 00:02:46,610
It can be ButtonState.pressed, or it can
be ButtonState.released.

41
00:02:46,610 --> 00:02:50,834
So I mentioned in passing in one of the
previous lectures, about enumerations

42
00:02:50,834 --> 00:02:55,810
and how they Are going to be very useful
to us in the future.

43
00:02:55,810 --> 00:02:58,632
Well now is the future, and so you should

44
00:02:58,632 --> 00:03:02,865
actually answer an in lecture quiz about
enumerations, and

45
00:03:02,865 --> 00:03:05,120
then we'll go back to the code.

46
00:03:08,570 --> 00:03:13,925
So now that we know.
That the left button property of the mouse

47
00:03:13,925 --> 00:03:21,130
state gives us either button state.pressed
or button state released.

48
00:03:21,130 --> 00:03:25,423
We see that we can actually compare the
current mouse state to,

49
00:03:25,423 --> 00:03:31,490
for a particular button to pressed or
released, and do something interesting.

50
00:03:31,490 --> 00:03:33,622
So I want to be clear, that this

51
00:03:33,622 --> 00:03:37,220
code is commented left mouse press code
bad.

52
00:03:37,220 --> 00:03:40,150
So that you don't think I thought this all
worked and everything.

53
00:03:40,150 --> 00:03:41,520
But here's what we're going to do.

54
00:03:41,520 --> 00:03:45,149
We're going to take the current state of
the mouse, we'll access the left button.

55
00:03:46,260 --> 00:03:50,930
We'll access the left button property and
then we'll compare it to pressed.

56
00:03:50,930 --> 00:03:54,098
And if the left mouse button is pressed
then

57
00:03:54,098 --> 00:03:59,026
we're going to randomly generate another
character number, and

58
00:03:59,026 --> 00:04:05,790
by the way the next method in the random
class The number we provide is.

59
00:04:05,790 --> 00:04:09,550
.
The number we provide is in fact.

60
00:04:12,090 --> 00:04:13,230
It's exclusive.

61
00:04:13,230 --> 00:04:16,390
It will not ever return four for this
call.

62
00:04:16,390 --> 00:04:19,840
It'll only return zero, one, two, or
three.

63
00:04:19,840 --> 00:04:22,050
So, that trips people up sometimes.

64
00:04:22,050 --> 00:04:23,070
So be aware of that.

65
00:04:23,070 --> 00:04:25,361
This upper bound, if you will, is

66
00:04:25,361 --> 00:04:29,460
a maximum, but it's an exclusive upper
bound.

67
00:04:29,460 --> 00:04:32,710
Okay, and then we use if statements like
you've seen before.

68
00:04:32,710 --> 00:04:34,452
So if the number we generated is

69
00:04:34,452 --> 00:04:37,199
zero, we're going to set the current
character to

70
00:04:37,199 --> 00:04:38,060
that sprite.

71
00:04:39,300 --> 00:04:42,790
Otherwise if it's one, we're going to do
one, otherwise, two, otherwise three.

72
00:04:42,790 --> 00:04:44,090
So that's how that code works.

73
00:04:45,350 --> 00:04:50,086
Now it turns out that the character
sprites that we're using here are not all

74
00:04:50,086 --> 00:04:55,310
the same within height.
So after we've picked the new texture 2D

75
00:04:55,310 --> 00:05:01,100
we want to modify the draw rectangle to
make it match the character's texture 2D.

76
00:05:01,100 --> 00:05:02,439
The new one we just picked.

77
00:05:03,850 --> 00:05:07,210
So, this is bad code, and let me show you
why.

78
00:05:08,710 --> 00:05:16,140
So we still get the drag around, but if I
press left, oh dear.

79
00:05:16,140 --> 00:05:18,462
I'm getting a bunch of different
characters, and that's kind

80
00:05:18,462 --> 00:05:20,840
of fun and I can drag them around and so
on.

81
00:05:20,840 --> 00:05:21,359
However,

82
00:05:23,680 --> 00:05:25,880
The problem is as follows.

83
00:05:25,880 --> 00:05:28,396
This update method right here in our game

84
00:05:28,396 --> 00:05:32,160
one class class gets called 60 times a
second.

85
00:05:32,160 --> 00:05:35,778
And so, if I'm holding the mouse down,
every 60th of a second

86
00:05:35,778 --> 00:05:40,440
it's saying yes the left mouse button is
pressed, so I should change.

87
00:05:40,440 --> 00:05:43,600
And that's why we're getting all those
changes that are happening.

88
00:05:43,600 --> 00:05:48,170
So what I need to do instead, is, well
I'll tell you what.

89
00:05:48,170 --> 00:05:49,136
Go do a in lecture

90
00:05:49,136 --> 00:05:51,320
quiz, and then come back.

91
00:05:51,320 --> 00:05:52,760
And then I'll tell you what we're going to
do.

92
00:05:59,050 --> 00:06:04,160
So, what we want to do, is we want to
change on a click.

93
00:06:04,160 --> 00:06:06,040
Not on a press.

94
00:06:06,040 --> 00:06:07,690
So how do we know about a click, what

95
00:06:07,690 --> 00:06:10,710
is a mouse click, what, what a
philosophical question.

96
00:06:10,710 --> 00:06:12,420
What is a mouse click?

97
00:06:12,420 --> 00:06:17,120
Well, a mouse click is a sequence of two
things.

98
00:06:17,120 --> 00:06:24,460
It's, the mouse was pressed previously,
and now it's released.

99
00:06:24,460 --> 00:06:28,420
And so, our, excuse me.
Our if statement cannot

100
00:06:28,420 --> 00:06:32,820
just be, if it's pressed, in fact, it
won't be if it's pressed.

101
00:06:32,820 --> 00:06:42,174
So what we really need, is we need if it's
currently released.

102
00:06:44,530 --> 00:06:48,610
And it was previously pressed.

103
00:06:48,610 --> 00:06:52,600
And here, you know, I'm poised to type,
but there's nothing to type.

104
00:06:52,600 --> 00:06:55,048
Because there's no way for me at this
point

105
00:06:55,048 --> 00:06:57,950
to know whether it was previously pressed
or not.

106
00:06:58,980 --> 00:07:01,080
The good news is, that we know how

107
00:07:01,080 --> 00:07:05,730
to store information, we store information
in variables.

108
00:07:05,730 --> 00:07:09,870
And so if we declare another variablbe to
keep track of what the previous

109
00:07:09,870 --> 00:07:11,780
button state was.

110
00:07:11,780 --> 00:07:17,880
Then we can use that to determine or to
detect when the mouse has been clicked.

111
00:07:17,880 --> 00:07:20,070
The left mouse button has been clicked.

112
00:07:20,070 --> 00:07:24,700
So, I'll declare a new variable here at
the top of the game one class.

113
00:07:24,700 --> 00:07:26,570
And it's going to be a button state
variable.

114
00:07:26,570 --> 00:07:29,690
These enumerations are a data type.

115
00:07:29,690 --> 00:07:32,900
And so we can declare variables of this
data type.

116
00:07:32,900 --> 00:07:35,240
So I'll call this previous button state,

117
00:07:35,240 --> 00:07:41,600
because it's the previous button state.
Some of you who type slowly or are lazy.

118
00:07:41,600 --> 00:07:43,311
Programmers are lazy in general,

119
00:07:43,311 --> 00:07:43,606
[LAUGH]

120
00:07:43,606 --> 00:07:45,435
but those of you who type slowly or are

121
00:07:45,435 --> 00:07:48,890
lazy my say PBS, means, you know, previous
button state.

122
00:07:48,890 --> 00:07:49,810
Don't do that.

123
00:07:49,810 --> 00:07:53,760
Use descriptive variable names because it
really helps with readability.

124
00:07:53,760 --> 00:07:54,460
All right.

125
00:07:54,460 --> 00:07:59,870
So we can pick between one of two possible
values to start with here.

126
00:07:59,870 --> 00:08:03,520
We can start the previous button state as
pressed or released.

127
00:08:03,520 --> 00:08:05,762
Well it is reasonable to assume when
somebody

128
00:08:05,762 --> 00:08:08,746
starts our game That they're not holding
down

129
00:08:08,746 --> 00:08:12,180
the left mouse button so let's say we
start at released.

130
00:08:13,500 --> 00:08:14,230
Okay.

131
00:08:14,230 --> 00:08:20,130
So we are now going to keep track of the
previous button state

132
00:08:20,130 --> 00:08:25,676
so that here in update we can now say, if
the button state

133
00:08:25,676 --> 00:08:31,000
is released and previous button state was
pressed.

134
00:08:34,530 --> 00:08:37,020
So now we've detected a click.

135
00:08:37,020 --> 00:08:43,450
It's now released and it was pressed on
the last update, then we'll do this stuff.

136
00:08:43,450 --> 00:08:46,780
So not too much of this stuff.
We'll do this stuff.

137
00:08:49,690 --> 00:08:53,590
So, I'll copy and paste it here, and I'm
going to comment this

138
00:08:53,590 --> 00:08:58,750
bad code out because otherwise it will
obscure what's really happening for us.

139
00:09:02,600 --> 00:09:05,309
So now, I'm checking if it's currently
released

140
00:09:05,309 --> 00:09:09,010
and it was previously pressed, change the
character.

141
00:09:09,010 --> 00:09:12,452
So now when I run the codes drags are on
like usual Oh

142
00:09:12,452 --> 00:09:19,140
dear I'm clicking away and nothing good is
happening, this is a debugging problem.

143
00:09:19,140 --> 00:09:19,460
Right.

144
00:09:19,460 --> 00:09:25,410
And the debugging problem is that
something is going around here obviously.

145
00:09:25,410 --> 00:09:25,690
Right.

146
00:09:25,690 --> 00:09:27,612
It's not catching or detecting that

147
00:09:27,612 --> 00:09:28,390
mouse click.

148
00:09:29,740 --> 00:09:33,244
Well you should realize with some thought
That

149
00:09:33,244 --> 00:09:36,940
previous button state is never going to be
pressed.

150
00:09:36,940 --> 00:09:40,790
Because, because we can find out why.

151
00:09:40,790 --> 00:09:44,606
I'm going to right click on this variable,
and then I'm going

152
00:09:44,606 --> 00:09:50,280
about halfway down, and I'm picking the
option that says find all references.

153
00:09:50,280 --> 00:09:52,791
When I do that, This window down

154
00:09:52,791 --> 00:09:59,270
on the bottom gives us every single
reference to this particular variable.

155
00:09:59,270 --> 00:10:02,610
And so, we're here, at well, we're here at
this one.

156
00:10:04,000 --> 00:10:07,540
If we double click the other one we see
we're checking against it.

157
00:10:07,540 --> 00:10:12,160
But if we look at those two things those
are the only two places in our entire game

158
00:10:12,160 --> 00:10:17,900
where this variable is accessed.
What that means though is this is the

159
00:10:17,900 --> 00:10:21,096
only assignment which in turn means this

160
00:10:21,096 --> 00:10:26,190
variable always says the button state is
released.

161
00:10:26,190 --> 00:10:28,230
So what we need to do is we need

162
00:10:28,230 --> 00:10:32,870
to update that previous button state on
every update.

163
00:10:32,870 --> 00:10:35,125
And we'll do this outside the if
statement.

164
00:10:35,125 --> 00:10:35,605
[SOUND]

165
00:10:35,605 --> 00:10:40,693
Really what we're saying is we want to
store this variable, we

166
00:10:40,693 --> 00:10:45,920
want to store the current state, into a
previous button state.

167
00:10:45,920 --> 00:10:47,720
Let me explain that in a minute.

168
00:10:47,720 --> 00:10:53,492
So previous button state is equal to
mouse.leftbutton So, basically what

169
00:10:53,492 --> 00:10:57,090
we're doing is, think of this as on the
first update.

170
00:10:57,090 --> 00:11:00,924
We come here, we get the current mouse
left button, and we store

171
00:11:00,924 --> 00:11:06,440
it into previous button state because
we're going to leave this update method.

172
00:11:06,440 --> 00:11:11,305
And then on a 30th, sorry, a 60th of a
second later, We're going to come

173
00:11:11,305 --> 00:11:14,225
back here again, and the previous button
state

174
00:11:14,225 --> 00:11:17,670
stores what the button state was last
update.

175
00:11:17,670 --> 00:11:22,464
And so in this way we can store basically
the button state from

176
00:11:22,464 --> 00:11:26,224
the last call to update so we can look at
it on this

177
00:11:26,224 --> 00:11:28,220
call to update.

178
00:11:28,220 --> 00:11:30,431
And now when I run the code you'll see if

179
00:11:30,431 --> 00:11:34,170
I click I get a change to a different
character.

180
00:11:34,170 --> 00:11:37,578
Now, if you're listening carefully to the
clicks, you

181
00:11:37,578 --> 00:11:41,970
may say well sometimes you're clicking and
its not changing.

182
00:11:41,970 --> 00:11:47,370
And that is actually not true, it just
appears to be that way.

183
00:11:47,370 --> 00:11:51,520
Here's the thing, when we generate a
random number here,

184
00:11:51,520 --> 00:11:55,270
say we're currently have displayed
character one.

185
00:11:55,270 --> 00:12:00,436
It's certainly possible, with a chance of
one in four, that we're going to

186
00:12:00,436 --> 00:12:06,210
generate a one again, and if we do, we do
this part, and then we move on.

187
00:12:06,210 --> 00:12:08,160
And so it's possible that on a click

188
00:12:08,160 --> 00:12:11,770
we get the same character we're currently
displaying.

189
00:12:11,770 --> 00:12:15,090
The code is not brokenm it's doing what we
said it should do.

190
00:12:15,090 --> 00:12:17,040
It randomly picks a character.

191
00:12:17,040 --> 00:12:20,940
But sometimes that random pick character
is the same character we currently have.

192
00:12:21,940 --> 00:12:26,090
The randomness is really an ineresting
thing in games.

193
00:12:26,090 --> 00:12:31,470
Because when we do things, purely,
randomly from a math perspective.

194
00:12:31,470 --> 00:12:36,610
It is easy sometimes, for players to
perceive that the game is broken.

195
00:12:36,610 --> 00:12:42,074
That it is not random, that there's a bug.
And if you had a coin flipping game, it is

196
00:12:42,074 --> 00:12:47,560
certainly within the realm of possibility
that you'd flip ten heads in a row.

197
00:12:47,560 --> 00:12:51,740
And that no gamer playing that game would
believe that you did it right.

198
00:12:51,740 --> 00:12:54,118
They would say it's broken, that can't be
done

199
00:12:54,118 --> 00:12:56,322
and you know the post on the forums and
you'll

200
00:12:56,322 --> 00:12:58,874
jump on and say obviously you don't
understand math

201
00:12:58,874 --> 00:13:02,100
and that just a ridiculous approach as a
game developer.

202
00:13:02,100 --> 00:13:02,790
Right?

203
00:13:02,790 --> 00:13:07,680
So what we do as game developers is we
actually don't do pure randomness.

204
00:13:07,680 --> 00:13:15,370
We do the appearance of randomness, such
that players like our game.

205
00:13:15,370 --> 00:13:19,060
And, you know, math purity can be saved
for the mathematicians.

206
00:13:19,060 --> 00:13:22,520
We fake randomness a lot.
I'm not going to fix that here.

207
00:13:22,520 --> 00:13:25,870
That's, sort of, something that you could
figure out on your own.

208
00:13:25,870 --> 00:13:32,289
And in fact, once we learn about iteration
in the next week, of lecturers.

209
00:13:32,289 --> 00:13:32,649
With,

210
00:13:32,649 --> 00:13:35,169
we'll actually see a way that we could, in
fact,

211
00:13:35,169 --> 00:13:38,760
make this better, so that it doesn't pick
the same character.

212
00:13:38,760 --> 00:13:41,116
But, for now, we'll just live with it, but

213
00:13:41,116 --> 00:13:44,039
understand that those things can in fact
be a problem.

214
00:13:45,300 --> 00:13:49,180
Alright, and that's it for the code for
today too, right.

215
00:13:49,180 --> 00:13:51,650
And in fact we're done with mice.

216
00:13:51,650 --> 00:13:53,690
We have learned how to use the mouse
location.

217
00:13:53,690 --> 00:13:56,210
We've learned how to use the mouse
buttons.

218
00:13:56,210 --> 00:13:57,684
We've learned that mouse

219
00:13:57,684 --> 00:14:01,980
clicks, and mouse presses, are in fact not
the same thing.

220
00:14:01,980 --> 00:14:05,828
And so, We are, next time, going to move
on

221
00:14:05,828 --> 00:14:10,770
to using 360 controllers for input in our
XNA games.