1
00:00:08,580 --> 00:00:13,030
Today we'll continue with our using the
controller

2
00:00:13,030 --> 00:00:16,120
as an input device in our XNA games.

3
00:00:16,120 --> 00:00:19,935
Here's a reminder of the module learning
objectives for Module eight.

4
00:00:23,310 --> 00:00:27,490
Last time we used a controller to ease the

5
00:00:27,490 --> 00:00:30,060
thumb stick on the controller to move our
character

6
00:00:30,060 --> 00:00:32,080
around, and keep it clamped in the window,
which

7
00:00:32,080 --> 00:00:34,510
had nothing to do with the thumb stick
itself.

8
00:00:34,510 --> 00:00:36,460
We just left that code alone.

9
00:00:36,460 --> 00:00:40,190
So this time we're going to move on and
use one of the buttons on the

10
00:00:40,190 --> 00:00:44,640
gamepad, or the controller, to change to a

11
00:00:44,640 --> 00:00:47,000
new character when the player presses that
button.

12
00:00:47,000 --> 00:00:48,438
So let's go to the code.

13
00:00:48,438 --> 00:00:50,950
We're starting from the code we ended with
last time,

14
00:00:54,220 --> 00:00:55,830
as you recall.

15
00:00:55,830 --> 00:00:59,050
We moved around with the thumb stick, and
we checked to

16
00:00:59,050 --> 00:01:01,840
make sure that the game pad was connected
before we did.

17
00:01:03,130 --> 00:01:10,610
Now, we're going to use the A button to
actually change characters.

18
00:01:10,610 --> 00:01:14,620
And so I'm going to take this closed curly
brace.

19
00:01:14,620 --> 00:01:15,900
Which goes with the if statement.

20
00:01:15,900 --> 00:01:19,400
Remember we said that we only want to use
gamepad input if the

21
00:01:19,400 --> 00:01:21,840
gamepad is connected.

22
00:01:21,840 --> 00:01:24,690
So, I'm going to delete that from there
and I'm going

23
00:01:24,690 --> 00:01:26,640
to move it all the way to the end of the
method.

24
00:01:30,880 --> 00:01:35,720
So now, any game pad processing we do will
only be done if the gamepad is connected.

25
00:01:37,890 --> 00:01:40,680
Let's comment this out, just to
demonstrate yet

26
00:01:40,680 --> 00:01:43,960
again that we can't use a press, that

27
00:01:43,960 --> 00:01:49,940
we really do need to use a click, even
with the A button on the game pad.

28
00:01:52,300 --> 00:01:53,530
So, uncomment this code.

29
00:01:55,840 --> 00:02:00,800
We of course, no longer have a mouse
variable.

30
00:02:00,800 --> 00:02:06,660
So let's say we'll change the comment, A
button press

31
00:02:06,660 --> 00:02:12,910
code bad, and so instead of

32
00:02:12,910 --> 00:02:18,259
checking whether the mouse left button is
pressed, we can check the gamepad.

33
00:02:19,830 --> 00:02:20,350
Recall,

34
00:02:21,490 --> 00:02:24,270
that this is a gamepad state object that

35
00:02:24,270 --> 00:02:27,340
we get by calling the gamepad get state
method.

36
00:02:28,620 --> 00:02:35,510
So we'll take a look at the gamepad, and
we'll access the buttons of the gamepad.

37
00:02:36,630 --> 00:02:39,550
And then we can pick which button we want
to access.

38
00:02:39,550 --> 00:02:42,340
So, there's a big button and there's, you
know,

39
00:02:42,340 --> 00:02:46,840
ab backstart, left shoulder is a button,
left stick

40
00:02:46,840 --> 00:02:49,050
means you push down on the left thumb
stick.

41
00:02:49,050 --> 00:02:50,800
So, we actually have quite a few buttons.

42
00:02:50,800 --> 00:02:54,450
I'll scroll here to show, we actually have
quite a few buttons

43
00:02:54,450 --> 00:02:57,840
on a 360 controller, but we're just going
to use the A button.

44
00:02:59,270 --> 00:03:00,700
And so when we F5, I

45
00:03:05,170 --> 00:03:08,290
press A and we get the same behavior we
had

46
00:03:08,290 --> 00:03:12,030
when we were trying to just do a left
mouse press.

47
00:03:12,030 --> 00:03:14,400
So that clearly doesn't work for us.

48
00:03:14,400 --> 00:03:16,920
And in fact I'm just going to delete all
this code.

49
00:03:22,640 --> 00:03:25,770
Perhaps with some control, I will delete
all this code.

50
00:03:27,650 --> 00:03:29,630
And I will uncomment this code.

51
00:03:30,870 --> 00:03:33,190
And we'll have to just have to make a
couple of changes to it.

52
00:03:33,190 --> 00:03:33,940
And we'll be good to go.

53
00:03:33,940 --> 00:03:41,310
Okay, so, uncomment.
Not cut, uncomment, well.

54
00:03:41,310 --> 00:03:47,090
All that code on the A button click, not
left mouse click.

55
00:03:47,090 --> 00:03:47,860
And it might feel

56
00:03:47,860 --> 00:03:50,660
to you that there's no reason to fix the
comments, but

57
00:03:50,660 --> 00:03:54,480
we want the comments to reflect how the
code actually works.

58
00:03:55,970 --> 00:03:57,590
So not the A button press.

59
00:03:59,610 --> 00:04:03,100
So we don't need this anymore, because
we're not using the mouse anymore.

60
00:04:05,410 --> 00:04:11,906
And here, we'll check gamepad,

61
00:04:11,906 --> 00:04:20,074
buttons.
A, just as before.

62
00:04:20,074 --> 00:04:23,320
So we're checking if A is released, we're
checking if the previous

63
00:04:23,320 --> 00:04:26,365
state was pressed, so this is a click, on
the A button.

64
00:04:26,365 --> 00:04:28,310
[NOISE]

65
00:04:28,310 --> 00:04:32,979
Change to a random character, and of
course we need to change this as well.

66
00:04:34,220 --> 00:04:36,360
To keep track of the previous state of the

67
00:04:36,360 --> 00:04:40,950
A button rather than the mouse left, left
mouse button.

68
00:04:40,950 --> 00:04:41,785
[LAUGH]

69
00:04:41,785 --> 00:04:47,800
So gamepad buttons, A, like so.
If I run it,

70
00:04:50,720 --> 00:04:54,940
I'm clicking the A button and sometimes I
get the same one.

71
00:04:54,940 --> 00:04:57,340
But regularly, I don't as well, and we

72
00:04:57,340 --> 00:04:59,480
talked about that last time, how that
works.

73
00:05:00,560 --> 00:05:04,270
So really, if we're doing a straight
conversion from the

74
00:05:04,270 --> 00:05:09,230
mouse left button to the gamepad A button,
that's it.

75
00:05:09,230 --> 00:05:12,440
That's all we need to do to get the same
behavior from

76
00:05:12,440 --> 00:05:15,810
the A button that we used to get from the
left mouse button.

77
00:05:15,810 --> 00:05:21,250
I do want to show you, though, that there
is another way that we can do this, that

78
00:05:22,570 --> 00:05:27,140
essentially uses the same idea, but in a
slightly different way.

79
00:05:27,140 --> 00:05:29,770
And so you might see lots of people
processing

80
00:05:29,770 --> 00:05:32,600
buttons with the game pad in this way
instead.

81
00:05:32,600 --> 00:05:34,170
So I'll show you this way as well.

82
00:05:34,170 --> 00:05:35,460
So I'm going to comment out this.

83
00:05:36,780 --> 00:05:41,970
And I'm going to replace it with a Boolean
variable, previous,

84
00:05:44,740 --> 00:05:51,920
A, I'm going to say aPreviousReleased, and
I'll set it to true.

85
00:05:51,920 --> 00:05:54,110
So essentially it's going to do the same
sort of

86
00:05:54,110 --> 00:05:56,930
thing, it's going to keep track of whether
the A button.

87
00:05:56,930 --> 00:05:58,290
Was previously released.

88
00:06:00,480 --> 00:06:04,810
So, since I described it that way, let's
change our variable name.

89
00:06:06,550 --> 00:06:09,070
So, aButtonPreviouslyReleased.

90
00:06:09,070 --> 00:06:12,790
Okay, we'll come down here, back to the
update code.

91
00:06:13,980 --> 00:06:16,140
And now we won't be checking this.

92
00:06:18,110 --> 00:06:22,250
We'll be checking
aButtonPreviouslyReleased.

93
00:06:22,250 --> 00:06:24,680
Now, notice there's no equal equal here.

94
00:06:24,680 --> 00:06:25,640
I could, in fact

95
00:06:25,640 --> 00:06:31,220
say equal equal true,

96
00:06:31,220 --> 00:06:36,570
for example, but there's no reason to do
that, because this is a Boolean variable.

97
00:06:36,570 --> 00:06:42,530
And so comparing a true or false to true
or false, just wastes cpu cycle.

98
00:06:42,530 --> 00:06:44,820
There's no reason to do it.

99
00:06:44,820 --> 00:06:47,800
As I think about this, as I copy this
here,

100
00:06:47,800 --> 00:06:50,790
I realize that I don't really care if the
A button

101
00:06:50,790 --> 00:06:54,800
was previous released.
What I really care about was, was

102
00:06:54,800 --> 00:06:58,830
it previously pressed?
And so in fact, I

103
00:06:58,830 --> 00:07:02,550
can do something called refactoring.
We've talked about refactoring as

104
00:07:02,550 --> 00:07:09,000
restructuring the code.
I can refactor to rename this variable.

105
00:07:09,000 --> 00:07:16,430
And if I say aButtonPreviouslyPressed and
say OK.

106
00:07:16,430 --> 00:07:17,490
And apply it.

107
00:07:17,490 --> 00:07:22,590
It changes it here, and it changes it
here.

108
00:07:22,590 --> 00:07:25,250
In fact it changes it everywhere
throughout my code.

109
00:07:26,750 --> 00:07:30,600
I actually want to now start though with
this being false.

110
00:07:30,600 --> 00:07:36,038
So A was not previously pressed, however
in our update.

111
00:07:36,038 --> 00:07:41,810
We'll check.
If the aButton is currently released and

112
00:07:41,810 --> 00:07:45,510
it was previously pressed then we'll do
this stuff.

113
00:07:45,510 --> 00:07:50,720
And in fact we will change this little bit
and instead of doing

114
00:07:50,720 --> 00:07:56,150
that comparison to the button state we'll
actually ask the game pad is.

115
00:07:57,650 --> 00:07:58,470
Button down.

116
00:08:01,040 --> 00:08:05,310
And we will have to provide it with one
argument, which button we mean.

117
00:08:06,730 --> 00:08:09,070
So we'll say Buttons.A.

118
00:08:09,070 --> 00:08:13,529
Buttons is just an enumeration of all the
possible buttons on the 360 controller.

119
00:08:14,730 --> 00:08:19,270
And so, this is a different way we check
and find out if A is down.

120
00:08:19,270 --> 00:08:23,650
But that's not what we really want.
We want if A is up, so it's released.

121
00:08:24,866 --> 00:08:26,100
And it was previously

122
00:08:26,100 --> 00:08:29,750
pressed so that's a click on the A button.
We still have

123
00:08:29,750 --> 00:08:33,800
to change this though, because we got rid
of that variable.

124
00:08:33,800 --> 00:08:40,433
So I'll just comment this out, and say
aPreviously,

125
00:08:40,433 --> 00:08:46,350
aButtonPreviouslyPressed is going

126
00:08:46,350 --> 00:08:52,311
to just be gamepad.IsButtonDown

127
00:08:52,311 --> 00:08:56,500
Buttons A.
So think about how this works.

128
00:08:57,780 --> 00:09:01,360
Basically, this is a Boolean variable, so
it has to get a Boolean value.

129
00:09:02,390 --> 00:09:05,920
The is button down method returns a
Boolean value,

130
00:09:05,920 --> 00:09:09,780
true or false, based on whatever button we
provided.

131
00:09:09,780 --> 00:09:13,600
So this question over here is, is the A
button down?

132
00:09:14,980 --> 00:09:18,120
And if this method returns true

133
00:09:18,120 --> 00:09:21,560
then the variable aButtonPreviouslyPressed
gets set to true.

134
00:09:21,560 --> 00:09:27,440
If this method returns false then this get
set to false, and so if we F5 again,

135
00:09:31,750 --> 00:09:35,450
I am getting the same behavior as before,
it's changing when I click the A button.

136
00:09:36,560 --> 00:09:41,800
The way that you do this which which
technique

137
00:09:41,800 --> 00:09:44,920
you use, whether you're doing is button
up, is button

138
00:09:44,920 --> 00:09:47,340
down or whether you're keeping track of
the button

139
00:09:47,340 --> 00:09:51,400
state of the buttons, is in many ways,
personal preference.

140
00:09:51,400 --> 00:09:54,070
I have personally found that I tend to use

141
00:09:54,070 --> 00:09:57,140
the is button up, is button down thing
more

142
00:09:57,140 --> 00:10:00,050
regularly when I'm processing a
controller.

143
00:10:00,050 --> 00:10:02,120
But, you can do it the other way, too.

144
00:10:02,120 --> 00:10:04,210
And if you do it the other way, it's

145
00:10:04,210 --> 00:10:08,220
so similar to the way we process mouse
input, mouse

146
00:10:08,220 --> 00:10:12,820
button input, that, that may be more
intuitive to you

147
00:10:12,820 --> 00:10:17,400
because you do it sort of consistently
across both input devices.

148
00:10:17,400 --> 00:10:22,280
So, what we've seen.
At this point,

149
00:10:22,280 --> 00:10:27,510
is we've seen how we can use the mouse,
both location and button.

150
00:10:27,510 --> 00:10:30,360
We've seen how we can use the controller,
both for the

151
00:10:30,360 --> 00:10:35,440
thumb stick and a button, as input devices
for our XNA games.

152
00:10:35,440 --> 00:10:40,490
And really that concludes in this module
all the forms of input.

153
00:10:40,490 --> 00:10:42,740
That we want to deal with.

154
00:10:42,740 --> 00:10:47,530
However, there is one form of output that
we can use with the

155
00:10:47,530 --> 00:10:48,185
360 controller.

156
00:10:48,185 --> 00:10:53,602
It's called forced feedback and we are
going to look at that next time.

157
00:10:53,602 --> 00:10:56,110
So we're all done with input, but we're
going to

158
00:10:56,110 --> 00:10:59,310
look at output next time to finish off the
module