⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch9.htm

📁 对于程序员来说可以利用JAVA来开发网络游戏!
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<P>
This <TT><FONT FACE="Courier">keyDown</FONT></TT> method shows
that handling different key presses is as easy as providing a
<TT><FONT FACE="Courier">switch</FONT></TT> statement with <TT><FONT FACE="Courier">case</FONT></TT>
clauses for each key. Although the example here used the <TT><FONT FACE="Courier">keyDown</FONT></TT>
method for handling key presses, the <TT><FONT FACE="Courier">keyUp</FONT></TT>
method works in the same fashion.
<P>
If you need more details about the key that was pressed or released,
you can use the <TT><FONT FACE="Courier">Event</FONT></TT> object
passed into the <TT><FONT FACE="Courier">keyDown</FONT></TT> and
<TT><FONT FACE="Courier">keyUp</FONT></TT> methods. The typical
usage of the <TT><FONT FACE="Courier">Event</FONT></TT> object
in regard to key processing is to check for modifier keys. Modifier
keys are keys that can be pressed in conjunction with other input
events, such as the Shift and Control keys. The following are
the three methods in <TT><FONT FACE="Courier">Event</FONT></TT>
used to check the status of modifier keys:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean shiftDown()<BR>
public boolean controlDown()<BR>
public boolean metaDown()</FONT></TT>
</BLOCKQUOTE>
<P>
All of these methods return boolean values specifying whether
or not the key in question is being held down. Checking the status
of the modifier keys is necessary sometimes in applets that make
heavy use of the mouse. For example, you might have a drawing
applet that performs a different function if the Shift key is
held down and the mouse is moved. You probably won't need the
modifier keys in Java games, but it is still important to know
how they work. Who knows, you might think of an interesting way
to incorporate them into a game.
<H2><A NAME="MouseEvents"><B><FONT SIZE=5 COLOR=#FF0000>Mouse
Events</FONT></B></A></H2>
<P>
Mouse events occur when the user moves the mouse or clicks the
mouse button. A handful of methods exist for handling mouse events,
such as the following methods:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean mouseUp(Event evt, int
x, int y)<BR>
public boolean mouseDown(Event evt, int x, int y)<BR>
public boolean mouseMove(Event evt, int x, int y)<BR>
public boolean mouseDrag(Event evt, int x, int y)<BR>
public boolean mouseEnter(Event evt, int x, int y)<BR>
public boolean mouseExit(Event evt, int x, int y)</FONT></TT>
</BLOCKQUOTE>
<P>
All of these methods are passed an <TT><FONT FACE="Courier">Event</FONT></TT>
object and two integer parameters representing the X and Y position
of the mouse pointer. The <TT><FONT FACE="Courier">mouseUp</FONT></TT>
and <TT><FONT FACE="Courier">mouseDown</FONT></TT> methods are
called when the user presses and releases the mouse button. The
<TT><FONT FACE="Courier">mouseMove</FONT></TT> method is called
when the mouse is moved. The <TT><FONT FACE="Courier">mouseDrag</FONT></TT>
method is very similar to the <TT><FONT FACE="Courier">mouseMove</FONT></TT>
method-the only difference being that <TT><FONT FACE="Courier">mouseDrag</FONT></TT>
is called when the mouse is moved with the button held down. The
<TT><FONT FACE="Courier">mouseEnter</FONT></TT> and <TT><FONT FACE="Courier">mouseExit</FONT></TT>
methods are used to track when the mouse enters and exits the
applet window.
<P>
You can use the <TT><FONT FACE="Courier">x</FONT></TT> and <TT><FONT FACE="Courier">y</FONT></TT>
parameters passed into the mouse event handler methods to perform
any processing based on the position of the mouse. The following
code snippet contains an example of overriding the <TT><FONT FACE="Courier">mouseMove</FONT></TT>
method to output the mouse position to standard output:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean mouseMove(Event evt, int
x, int y) {<BR>
&nbsp;&nbsp;System.out.println(&quot;Mouse position = (&quot;
+ x + &quot;, &quot; + y + &quot;)&quot;);<BR>
&nbsp;&nbsp;return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
Similar to the keyboard event handlers, you can use the <TT><FONT FACE="Courier">Event</FONT></TT>
object passed in the mouse event handlers to find out additional
information such as the status of modifier keys.
<H2><A NAME="SampleAppletFlyingSaucer"><B><FONT SIZE=5 COLOR=#FF0000>Sample
Applet: Flying Saucer</FONT></B></A></H2>
<P>
Now that you have a good idea of how to process user input events
in Java, let's take a look at a sample applet that uses this newfound
knowledge. The Flying Saucer applet uses the sprite classes and
event handler methods to implement a user-controllable flying
saucer. Figure 9.1 shows what Flying Saucer looks like. The complete
source code, executable, and images for the Flying Saucer applet
are included on the accompanying CD-ROM.
<P>
<A HREF="f9-1.gif" ><B>Figure 9.1 : </B><I>The Flying Saucer sample applet.</I></A>
<P>
The <TT><FONT FACE="Courier">FlyingSaucer</FONT></TT> class models
the applet itself and takes care of all the details related to
setting up the sprite classes and handling the user input events.
Similar to the other applet classes you've developed that use
sprites, <TT><FONT FACE="Courier">FlyingSaucer</FONT></TT> contains
familiar support for the sprite classes. In fact, the only significantly
new code in the <TT><FONT FACE="Courier">FlyingSaucer</FONT></TT>
class is the code for handling user input.
<P>
Before getting into the specifics of the user input handlers,
however, take a look at two of the member variables defined in
the <TT><FONT FACE="Courier">FlyingSaucer</FONT></TT> class:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">private Sprite theSaucer;<BR>
private int    lastKey;</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">theSaucer</FONT></TT> member variable
is a <TT><FONT FACE="Courier">Sprite</FONT></TT> object that holds
the flying saucer sprite. It is necessary to keep up with this
sprite outside of the sprite list because you need to be able
to alter its position and velocity based on user input events.
The <TT><FONT FACE="Courier">lastKey</FONT></TT> member variable
is used to hold the value of the last key pressed. This variable
is used to provide finer control over the flying saucer, as you'll
see later in this lesson.
<P>
The <TT><FONT FACE="Courier">keyDown</FONT></TT> method handles
all the details of supporting keyboard control of the saucer.
Listing 9.3 shows the source code for the <TT><FONT FACE="Courier">keyDown</FONT></TT>
method.
<HR>
<BLOCKQUOTE>
<B>Listing 9.3. The </B><TT><B><FONT FACE="Courier">FlyingSaucer</FONT></B></TT><B>
class's </B><TT><B><FONT FACE="Courier">keyDown</FONT></B></TT><B>
method.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean keyDown(Event evt, int
key) {<BR>
&nbsp;&nbsp;// Change the saucer velocity based on the key pressed
<BR>
&nbsp;&nbsp;Point vel = theSaucer.getVelocity();<BR>
&nbsp;&nbsp;switch (key) {<BR>
&nbsp;&nbsp;case Event.LEFT:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;vel.x = -4;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (lastKey == Event.LEFT)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vel.y = 0;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;case Event.RIGHT:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;vel.x = 4;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (lastKey == Event.RIGHT)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vel.y = 0;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;case Event.UP:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;vel.y = -4;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (lastKey == Event.UP)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vel.x = 0;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;case Event.DOWN:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;vel.y = 4;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (lastKey == Event.DOWN)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vel.x = 0;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;default:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;vel.x = vel.y = 0;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;theSaucer.setVelocity(vel);<BR>
&nbsp;&nbsp;lastKey = key;<BR>
&nbsp;&nbsp;return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The <TT><FONT FACE="Courier">keyDown</FONT></TT> method first
gets the current velocity of the saucer and checks to see which
key was pressed. It then alters the saucer's velocity according
to the directional arrow key pressed. The <TT><FONT FACE="Courier">lastKey</FONT></TT>
member variable is then checked to see whether this is a repeat
key press. If so, the tangential velocity component is cleared.
For example, if the left arrow key is held down, the Y velocity
component is cleared. This has the result of causing the saucer
to change from moving in a diagonal direction to moving in a pure
X or Y direction if you hold a key down, which gives the keyboard
controls a better feel. Try it out for yourself.
<P>
The <TT><FONT FACE="Courier">mouseDown</FONT></TT> and <TT><FONT FACE="Courier">mouseDrag</FONT></TT>
methods are used to handle mouse input events and position the
saucer at an absolute location:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean mouseDown(Event evt, int
x, int y) {<BR>
&nbsp;&nbsp;theSaucer.setPosition(new Point(x - (saucerSize.width
/ 2),<BR>
&nbsp;&nbsp;&nbsp;&nbsp;y - (saucerSize.height / 2)));<BR>
&nbsp;&nbsp;return true;<BR>
}<BR>
<BR>
public boolean mouseDrag(Event evt, int x, int y) {<BR>
&nbsp;&nbsp;theSaucer.setPosition(new Point(x - (saucerSize.width
/&nbsp;2),<BR>
&nbsp;&nbsp;&nbsp;&nbsp;y - (saucerSize.height / 2)));<BR>
&nbsp;&nbsp;return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
Both of these methods simply reposition the saucer at a location
centered on the current mouse position, which enables you to click
and drag the saucer around with the mouse. This might not be an
ideal usage of the mouse in most game scenarios, but it shows
how the mouse can be used to control a sprite, which can be useful.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
You might be thinking that the duplicate code in the <TT><FONT FACE="Courier">mouseDown</FONT></TT> and <TT><FONT FACE="Courier">mouseDrag</FONT></TT> methods goes against good programming practice. You're right! The truth is that I didn't want to confuse 
things by having these methods call a third method, which is typically the way you avoid duplicate code in a situation like this. You might also think that <TT><FONT FACE="Courier">mouseDrag</FONT></TT> could just call <TT><FONT 
FACE="Courier">mouseDown</FONT></TT> and simply pass its parameters along. Although this technique would work in this particular case, it's generally not a good idea to directly call event handler methods yourself, primarily because the <TT><FONT 
FACE="Courier">Event</FONT></TT> parameter means different things to different event handlers.
</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>
</H2>
<P>
In this lesson, you learned about Java events and the event-driven
architecture necessary to support them. More specifically, you
learned about Java input events, including the input devices capable
of generating them and how they are handled by the Java awt library.
<P>
You saw examples of using the input event handler methods to capture
and respond to keyboard and mouse events. You then finished up
the lesson with a sample applet using the sprite classes that
implements a user-controllable flying saucer supporting both keyboard
and mouse input. This sample applet brought you yet another step
closer to implementing a complete game. As a matter of fact, you're
now ready to embark on your first complete Java game; the next
lesson focuses on developing your first full-blown Java game,
Traveling Gecko.
<H2><A NAME="QA"><B><FONT SIZE=5 COLOR=#FF0000>Q&amp;A</FONT></B></A>
<BR>
</H2>

<TABLE>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>What's the big deal with event-driven programming?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Event-driven programming provides a powerful methodology for handling the complexities inherent in a graphical system. By modeling every action in the system as an event with a corresponding handler, the 
complexities are broken down into individually serviceable items.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>What are Java input events?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>They are any events generated by the user manipulating an input device such as the mouse or keyboard.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>What is the purpose of the <TT><B><FONT FACE="Courier">handleEvent</FONT></B></TT> method?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>The <TT><FONT FACE="Courier">handleEvent</FONT></TT> method acts as a router method for all events. All events must pass through <TT><FONT FACE="Courier">handleEvent</FONT></TT>, which in turn calls the 
appropriate event handler method.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>If the user has more than one mouse button, can I detect when the user presses one of the extra buttons?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50> <B>A</B></TD><TD>No, Java only supports single-button mice. This is to eliminate the creation of extra button features that wouldn't be available to users (such as Macintosh users) with one button on their mice.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><B><FONT SIZE=5 COLOR=#FF0000>Workshop</FONT></B></A>
</H2>
<P>
The Workshop section provides questions and exercises to help
strengthen your grasp on the material you learned today. Try to
answer the questions and at least put some thought into the exercises
before moving on to tomorrow's lesson. You'll find the answers
to the questions in appendix A, &quot;Quiz Answers.&quot;
<H3><A NAME="Quiz"><B>Quiz</B></A></H3>
<OL>
<LI>What is an event?
<LI>How do you determine whether the user is holding down the
Shift key while moving the mouse?
<LI>How can you detect when the mouse has been moved outside the
applet window?
<LI>How is the saucer controlled in the Flying Saucer applet?
</OL>
<H3><A NAME="Exercises"><B>Exercises</B></A></H3>
<OL>
<LI>Think of some things that can take place in a Java applet
that might generate events.
<LI>Think of some popular games and how the user input is handled
for each. Do they support the mouse? If so, how?
<LI>Add a hyperspace feature to the Flying Saucer applet. A hyperspace
feature would result in the saucer moving to a random location
if a certain key, such as the spacebar, is pressed.
<LI>Change the saucer in the Flying Saucer applet to a frame-animated
sprite.
</OL>
<P>
<HR WIDTH="100%"></P>

<CENTER><P><A HREF="ch8.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch10.htm"><IMG 
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>

<P>
<HR WIDTH="100%"></P>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -