Sie sind auf Seite 1von 5

So, we're halfway through module 11.

Continuing along, here are the learning


objectives for the module. Last time we finished up with our console
dye class and so it's time for us to move
forward and to develop a class that is designed
specifically for use in XNA game rather than in a console
application. So, in this lecture we will be designing
that class and we'll be implementing the fields and properties,
so the state, again in that class. Before we do, you should go take an
in-lecture quiz. Now I know we've discussed using a rubber
chicken as a weapon before, way back in module four.
I have cut back a little bit on the scope of that weapon so it can't be used for
, as
melee weapon, it's only a ranged weapon. So we'll use it as a ranged weapon and
we're going to focus today, well, we'll do all the design but we'll
focus on implementing the state stuff. So here are the things that we really nee
d
to keep track of for the state. We need to keep track of whether or not
the rubber chicken is active. Because we're going to use it as a weapon,
essentially a projectile, and so when the projectile destroys something we shoul
d
probably destroy the projectile as well. So we're going to deactivate the rubber
chicken just as we deactivated teddy bears when they got destroyed. We're also g
oing to need other two things
here. We're going to need both a sprite so we
can draw the rubber chicken in our XNA game and a
draw rectangle. Now, as I've mentioned in passing before,
there are actually lots of ways to draw sprites. The Sprite Batch draw method ha
s lots of
overloads. We're going to continue using a draw
rectangle roll. And I said draw slash collision rectangle because inside
the class we'll think of it and refer to it as the draw
rectangle. But again, just as with the teddy bear,
we're going to consider consumers of the class will think about
that rectangle as a collision rectangle. So we'll handle that when it's time to
build the appropriate property. Rubber chickens that are used as ranged
weapons will have a velocity once they are
launched and so we need that velocity so we can update the rubber chicken's posi
tion, or
location, over time. And then finally, the amount of damage
that the rubber chicken inflicts. So when we hit something with the rubber,
when we hit something with the rubber chicken it causes some damage and maybe so
me rubber chickens are more
powerful than others and so we'll keep track of the
damage that we can apply as well. Because, we're going to look at the UML
for the fields in a moment, but because we're going to save
all this state information inside the object as fields you should go do an
in-lecture quiz telling me that you understand our conversation about
fields when we did the console class. So here's the UML. And it should come as n
o surprise to you
that we have five different fields here that are going to store the five pieces
of
state that we talked about before. The additional detail that's included in
here is the data types for each of those pieces of
state. So active will be a bool, true or false,
right? That makes sense. It's either active or it isn't.
The damage will be an integer amount. We won't need the granularity that says it
does, you know, 48.2 or anything like
that. The draw rectangle will be just as we've been doing before, it will be a r
ectangle
object. The sprite will be a Texture2D, as we're
used to in XNA games, and the velocity will be a
Vector2. So as a reminder, a vector2 has two components, or two properties that
we
can access. It has an X, and it has a Y. So if we have this two dimensional vect
or
is really what it is then it will tell us,
what's the velocity in the x direction, positive
or negative, and what's the velocity in the y direction,
positive or negative. So those are, in addition to the field names, we now have
captured the data types
we're going to use for those fields which helps
us when we get to the implementation part. So I'm going to actually start a new
project. So I've double-clicked the, my, my
shortcut and now I'm going to create this new project. So I say new project, it
is an XNA game
studio game, it is a Windows game. I'm going to browse to the place where I
want to save it. So I'll cut here and be back momentarily. Okay, I have saved it
and we have the code here. And I'll zoom in even though we're going
to immediately go and create a new class. And so we do this exactly the same way
that we did it in the console application. You need to be a little careful. Make
sure you don't create the class down
here in the content area. That's for content not code. You want to create a big
class up here where we're storing all of our source
code. All of our C sharp files.
So we right click, I've named this namespace rubber chicken by the
way, I right click it. I say that I wants to add a class and my
class is going to be a rubber chicken. [SOUND] I'll zoom in immediately and put
the class
comment. [SOUND] And I'll add a region for Fields. [SOUND] And I'm going to sort
of group these together as we go along for, you know, drawing support and movem
ent support and so on but you can put these fields in any order you want really.
And, and some I won't even comment. So typically I comment the drawing support
fields even though there's only two of them but I don't necessarily
comment the active field, for example. So, I actually will put that active field
,
remember we decided it was going to be a Bool and to this one we can
just initialize to true. Just like at the end of the lecture last
time we had a random number generator that we made a
field in the die class instead of just using a local variable, that's what a var
iable
that's declared inside a method is called, because it's local to
the method. Instead of using a local variable we used
a field and we initialized it when we
declared it. And so that's a perfectly valid approach
to use and so I'm setting active to true here
because rubber chickens should always start as active.
Okay, I'll add some drawing support. So I need a Texture2D and I know that it's
hard to see and there's no way for me to zoom in on the intellesense but Texture
2D
is not showing up as one of my choices. What that means is that I am not, I do n
ot
currently have direct access to that class.
Well, we, we have seen this in the programming assignments where
you needed to add a using statement and so we need to add a using statement to
get at Texture2D. So we need to type in using whatever the
name space Texture2D appears in and you may not have memorized yet what
name space that's in. Good news is we can go to help and we can
search on Texture2D, and we will sort of click
around for awhile until we get to where we need to be but the
Texture2D class, it says right up here at the top, which
namespace it appears in. And so the Texture2D is found in the
Microsoft dot xna dot framework dot graphics
namespace so that's the namespace we need to add. So Microsoft and Intellisense
will help us
a little bit here, graphics. Now that I've added that namespace, when I
come back here, I hit Ctrl Space, by the way, to make
Intellisense pop back up. Now all those textures show up, which is
good. So, I'll need a sprite and I'll also need
a rectangle for the draw rectangle and we say well, that
one's not showing up either. So unless you've memorized which namespace
rectangle appears in, back we go to Help. [SOUND] And I can click on any of thes
e until I can
click on the Rectangle Structure and this one is
found in the Framework namespace. Not Microsoft dot XNA dot Framework dot
Graphics. Just Microsoft dot XNA dot framework. So back to the code again.
Another using statement. [SOUND] And now again, control space and rectangle is t
he only one that fits so
it automatically filled it in for me and we now have our draw rectangle.
So, that's good. We have three of the fields taken care of
already. We still need to handle damage and
velocity. So let's take care of [SOUND] damage first.
And again, your order doesn't matter. I'm just sort of grouping them in a way
that seems intuitive to me. So we'll say there's some damage field
we're going to have. And I'll even change this comment to say
drawing and moving support so I can just put the vector two that I need for my
velocity right here as well. And that's it for the fields.
We need an active field, that's a Boolean. Damage, that's an INT.
Sprite is a Texture2D. Draw rectangle is a rectangle and velocity
is that two dimensional vector that we talked
about. So now it's time to actually start adding
the properties. So first, go do an in-lecture quiz about
properties. And now that you're back you can see that
I've added properties here to the UML. So these are the three properties that we
need. We need active. And we talked about properties last time. By the way, prop
erties can be read, they
can be write, or they can be read-write depending on the combination
of get accessors and set accessors that we
provide. And so in this particular case, for the
Active property, we've said it's going to be Active with a
capital A. We're going to provide both a get
accessor, so the game can determine whether or not
the rubber chicken is active, and a set
accessor, so that the game that's using the rubber
chicken can deactivate the rubber chicken when it
should be destroyed. And finally that last little piece right
here says that it is in fact going to be a bool
property. Which makes sense right? Because the active field is a Boolean, it
can either be true or false, it makes sense that the property is
a Boolean as well. Our second property, the collision
rectangle. Remember, I have renamed what we're
calling draw rectangle in our field to be collision rectangle for the
property because the consumer of the property is really thinking of it as
something to be used for collision detection not something to
be used for drawing the sprite. Because that's internal to the class so a
consumer of the class doesn't care. It is a get accesser only. It makes no sense
for the game to be able to change the collision rectangle for
this thing. It's based on the sprite and I'll, well,
it's based on the sprite and the draw rectangle so only read
access for this particular property. And then finally we have the damage
property and integer as, as we'd expect. Only a get accessor so only a read, rea
d
access for this property as well. So there are no power ups that will make
the rubber chicken inflict more damage. This will be a really basic rubber
chicker, chicken game. Not the deep play experience that you
might have been expecting. Alright, so back to the code, I am adding a region fo
r those properties that we were
juts talking about, and I'll add them one at a
time. So, we'll make them public. Remember the access modifier. This particular
property, I'm doing active
first, so it's a bool and Active with a capital A. And I said that we were going
to provide
both get and set accessors. And I'll add the comment now before I
forget. Gets and sets whether the rubber chicken
is active. So re, recall from the other properties
that we have done so far that we're going to end up
returning the value of the active field. And to return things we type return. Th
e set accessor, which we looked at
before and I showed you an example and then got rid of, looks similar
to the example I showed you. We're going to be setting the active field
equal to whatever value is provided when the game accesses this property, this
set accessor for the property. Remember, it will say something like
rubber chicken zero dot active equals false.
And so inside here, inside the set accessor, that false is value.
Alright, so that's the first property. The second property is collision
rectangle. So, public again. This time it's a rectangle. [SOUND] And recall, thi
s one we're only providing
a get accessor for. [SOUND] And here's where using properties rather
than just giving consumers of the class direct access to the field is
useful because we can rename this to Collision Rectangle
instead of Draw Rectangle based on how the consumer of the class thinks
about this property. Gets the collision rectangle for the
rubber chicken, and finally, we had one more property in
our UML, and that property was Damage. So it's public int Damage. [SOUND] Get ac
cess here only. Gets the damage rubber chicken inflicts.
And get works exactly like the other get accessors we've been doing.
We just return damage. So now we have the properties included
inside our implementation. we, just as with the dye class after we did fields an
d properties, we couldn't
do any testing on it yet, and we can't here either because we can't construct an
y
rubber chickens yet. But we'll be able to do that next lecture
and we'll implement the constructor and then we can construct and
make sure they work and so on. So, we'll get there, we're just not there
quite yet. Okay. So back to the slides. The last piece of the design, I know I
sort of snuck in some implementation during the
design, and that's okay too. We, it is very rare to do all your design
and then all your implementation. This is a really iterative thing. We design so
me, we implement some, we
design some, we implement some. And sometimes we go back after starting to imple
ment and say
wow our design was not correct and we need to fix
it. And so this going back and forth is a
really common thing. Nothing wrong with it. But now that we're back here on the
slides
talking about design we have behavior for the
rubber chicken too. And I have simplified that behavior as
well, I, we, talked back in module four about playing
animations and so on. We won't do that here. What we'll do instead is we will do
two things. We will update the rubber chicke, and this
one will be a fairly interesting and reasonably complicated thing because we'll
move it along its velocity vector based on the elapsed
time. That's not the complicated part. And we will also process mouse input. So
the way we're going to launch rubber
chickens, I mean, you, you, I think we'd have rubber chicken cannons
or something like that because it's really a projectile.
But we're not going to have that. We're, in fact, going to launch the rubber
chicken by clicking on it. Now this is a very stupid rubber chicken. It's not a
homing rubber chicken or
anything. When we click on it it's just going to go straight up is how this rubb
er chicken
will work. If you wanted to, you could of course implement far more complicated
schemes where you click a rubber chicken to activate it and
then click somewhere on the screen and that's where it's going to
launch itself toward and so on. You know, send it, send the rubber chicken
over there. Right? And th, and that's a perfectly reasonable
thing to do that obviously people have figured out how
to do in games. We're just not going to do it here today.
We're not going to do it at all. But we'll do a really simplified rubber
chicken goes up when we launch it. So that's this part. So we're going to have t
o detect a mouse
click, not a mouse press, a mouse click on the
chicken. And we've learned how to do that already
when we were processing mouse buttons in the X and A input lectures. And the oth
er behavior we're going to have
is we're going to have the rubber chicken
draw itself. And that one's going to be a really easy
one to do. So if we look at the final UML for our diagram from today's
lecture we see that we've added these methods down
here. And so we also have identified the fact
that to be able to draw we're going to need a sprite batch
to get passed in. So we can call the sprite batch dot draw
method. And to update, because there's both the velocity which needs the elapsed
game time and the click processing which needs
the mouse, we'll need both of those things.
Just like our roll method had a return type of void both of these methods also
have a return type of void. So we're not writing any methods in this
class that will actually return a value when we
call it. And that's all we're going to do for
today. We did all of our design work and we did our implementation work for the
fields
and properties so we've got off to a great start on our
rubber chicken for the X and A game. And next time, we'll actually implement
both the constructor and the two methods that we need for the behaviors
of the rubber chicken.

Das könnte Ihnen auch gefallen