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

📄 117-123.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Adding Interactivity</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=4//-->
<!--PAGES=117-123//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch03/111-116.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="123-127.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 4<BR>Adding Interactivity
</FONT></H2>
<P><I>Joel Fan</I></P>
<P><FONT SIZE="+1"><B>Goals:</B></FONT></P>
<P>Understand input devices and Java&#146;s event handling
</P>
<P>Learn more about bitmaps</P>
<P>Create interactive applets</P>
<P>Interactivity is a critical element in games. Your users must be able to act and react within the game world. (Otherwise, it&#146;s a simulation, not a game!) In this chapter, you will learn how to add interactivity to your applets by allowing players to manipulate <I>input devices</I>, primarily the mouse and the keyboard. The key is understanding how the Java Abstract Windowing Toolkit (AWT) structures and handles <I>events</I>.</P>
<P>So far, you have learned how to animate sprites that operate on their own after they have been created. Now, you&#146;ll see how applets and sprites can respond to external input. You&#146;ll also learn how to display text and use bitmaps in a variety of ways. The final applet of the chapter uses event handling and bitmap animation so you can fly a UFO in space! To begin, let&#146;s talk about input devices.</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">How Input Devices Work</FONT></H3>
<P>An input device allows information from the outside world to be transmitted to the computer. The Java AWT supports the two input devices&#151;keyboard and mouse&#151;that are available on practically every computer. Each device has different uses in game design.
</P>
<P>The keyboard is ideal for inputting text, of course, and letting the player control multiple activities concurrently. Anytime an arcade game uses buttons, for example, it can be adapted to a keyboard. There&#146;s an obvious limit to how many buttons you can manipulate at once&#151;humans usually have ten fingers, and controlling five or six buttons simultaneously is probably at the limit for (human) players with average dexterity. (Aliens may not be as limited!) Although the keyboard is good for inputting multiple binary values, which is sufficient for lots of games, it&#146;s not the best device when you need to navigate a two-dimensional space. This is a job for a mouse.</P>
<P>There are two basic ways of mapping mouse motion into a 2D playing field:</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;One method treats the mouse as an absolute pointer into 2D space, in the same way as windowing systems, such as Windows 95 or the MacOS. This approach is well suited for games where the playing field is limited to the size of a particular window, like card games, or board games such as chess and Monopoly.
<DD><B>&#149;</B>&nbsp;&nbsp;The second method interprets the mouse move or location as an indicator of relative motion. This way, the mouse can navigate a game world that exists beyond the confines of the window. For example, DOOM, which has a 2D playing field (although the graphics are 3D), uses the mouse in this way. When you pull the mouse toward you, your character appears to move forward.
</DL>
<P>Figure 4-1 illustrates these two ways of using the mouse in games.
</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/04-01.jpg',560,379 )"><IMG SRC="images/04-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-01.jpg',560,379)"><FONT COLOR="#000077"><B>Figure 4-1</B></FONT></A>&nbsp;&nbsp;Using the mouse in games</P>
<P>A mouse also has one or more buttons that might trigger actions such as firing a gun, jumping into hyperspace, or betting all your money.
</P>
<P>Sometimes, it&#146;s not obvious which input device&#151;keyboard or mouse&#151;is better for the game you&#146;re writing. One solution is to permit both forms of input and let the player decide. But try to stay away from requiring your players to manipulate both mouse and keyboard rapidly and simultaneously&#151;it&#146;s like asking some people to rub their heads while patting their stomachs!</P>
<P>Now, let&#146;s see how mouse and keyboard input gets transmitted to an applet. Such input is an example of an <I>event</I>.</P>
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">What Is an Event?</FONT></H3>
<P>An event is an action such as a keypress or a mouse move. When you manipulate an input device, such as the mouse or keyboard, you trigger an event. And by providing the appropriate event <I>handlers</I>, you can determine what actions a program will take when an event occurs. An event handler is simply a method that you provide in an applet class. As you can guess, events and event handling are crucial in creating games that respond to people.</P>
<P>As a Java game designer, your task of handling events is considerably simplified by the AWT, which does the dirty work of encapsulating events as objects and passing these event objects to the appropriate handlers. The following is a high-level view of what happens:</P>
<DL>
<DD><B>1.</B>&nbsp;&nbsp;The player triggers an event through the mouse or keyboard.
<DD><B>2.</B>&nbsp;&nbsp;The event is wrapped as an object and passed to the appropriate event handler. This step happens automatically. (Sometimes, associated parameters, such as the location of the event, are passed as well.)
<DD><B>3.</B>&nbsp;&nbsp;The event handler method, which you provide, takes action based on the contents of the event object.
</DL>
<P>Before we go any further, let&#146;s distinguish between events triggered through the mouse and events initiated from the keyboard.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Mouse Events</FONT></H4>
<P>Here&#146;s an illustration of a mouse event. Let&#146;s say an applet is running, and you press the mouse button inside the applet&#146;s bounding rectangle. You&#146;ve triggered a <I>mouseDown</I> event. An Event object is created and passed, along with the mouse&#146;s location, to the appropriate event handler. This handler is the mouseDown() method, which is inherited by your applet class. The default mouseDown() does nothing, but by overriding it in your applet, you can specify what happens. Figure 4-2 summarizes how the mouseDown event gets passed to your applet.</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/04-02.jpg',562,381 )"><IMG SRC="images/04-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-02.jpg',562,381)"><FONT COLOR="#000077"><B>Figure 4-2</B></FONT></A>&nbsp;&nbsp;mouseDown event</P>
<P>Of course, mouseDown isn&#146;t the only mouse input event. Some others that the AWT also recognizes are <I>mouseUp</I>, which occurs when the mouse button is released, <I>mouseMove</I>, triggered if the mouse changes location, and <I>mouseDrag</I>, which is a combination of depressing the button and moving the mouse (i.e., dragging the mouse). To handle these events, all you need to do is override the appropriate method. Table 4-1 is a list of mouse events and the methods that get called when they occur.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 4-1</B> Mouse events
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="55%" ALIGN="LEFT">Event
<TH WIDTH="45%" ALIGN="LEFT">Method
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">mouseDown (mouse button pressed)
<TD>public boolean mouseDown(Event e,int x,int y)
<TR>
<TD VALIGN="TOP">mouseDrag (mouse moves while button down)
<TD>public boolean mouseDrag(Event e,int x,int y)
<TR>
<TD VALIGN="TOP">mouseEnter (mouse enters applet region)
<TD>public boolean mouseEnter(Event e,int x,int y)
<TR>
<TD VALIGN="TOP">mouseExit (mouse exits applet region)
<TD>public boolean mouseExit(Event e,int x,int y)
<TR>
<TD VALIGN="TOP">mouseMove (mouse moves while button up)
<TD>public boolean mouseMove(Event e,int x,int y)
<TR>
<TD VALIGN="TOP">mouseUp (mouse button up)
<TD>public boolean mouseUp(Event e,int x,int y)
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch03/111-116.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="123-127.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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