📄 177-179.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:Building a Video Game</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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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=5//-->
<!--PAGES=177-179//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="171-177.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="179-185.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Notice that the missile is fired by calling its init() method. This is much faster than creating a new MissileSprite object for each mouse click, and it illustrates another general rule when writing games<I>: Avoid dynamic allocation of objects during game play</I>. Try to allocate all the objects you will use at the very beginning, if possible, so the runtime system doesn’t need to construct one when the game is running.</P>
<P>Now, let’s create the aliens!</P>
<H3><A NAME="Heading22"></A><FONT COLOR="#000077">Defining the UFOManager</FONT></H3>
<P>The UFOManager is responsible for initializing the individual UFO sprites, and telling them when to paint and update. Let’s create the UFO class first, before defining UFOManager<I>.</I></P>
<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">The UFO Class</FONT></H4>
<P>The UFO class will animate the sequence of bitmaps shown in Figure 5-2, so it becomes a subclass derived from the BitmapLoop sprite, which we introduced in Chapter 4, Adding Interactivity.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">The BitmapLoop Sprite Class</FONT></H4>
<P>Listing 5-10 shows the current definition of BitmapLoop.
</P>
<P><B>Listing 5-10</B> BitmapLoop class</P>
<!-- CODE //-->
<PRE>
class BitmapLoop extends BitmapSprite implements Moveable{
protected Image images[]; // sequence of bitmaps
protected int currentImage; // the current bitmap
protected boolean foreground; // are there foreground images?
protected boolean background; // is there background image?
// constructor. Assumes that background image is already
// loaded. (use MediaTracker)
public BitmapLoop(int x,int y,Image b,Image f[],Applet a) {
super(x,y,b,a);
if (image != null) { // if there's a background image
background = true;
}
else {
background = false;
}
images = f;
currentImage = 0;
if (images == null || images.length == 0) {
foreground = false; // nothing in images[]
}
else {
foreground = true;
if (!background) { // if no background
width = images[0].getWidth(a); // get size of images[0]
height = images[0].getHeight(a);
}
}
}
// cycle currentImage if sprite is active, and there
// are foreground images
public void update() {
if (active && foreground) {
currentImage = (currentImage + 1) % images.length;
}
updatePosition();
}
public void paint(Graphics g) {
if (visible) {
if (background) {
g.drawImage(image,locx,locy,applet);
}
if (foreground) {
g.drawImage(images[currentImage],locx,locy,applet);
}
}
}
// implement moveable interface
public void setPosition(int x,int y) {
locx = x;
locy = y;
}
protected int vx;
protected int vy;
public void setVelocity(int x,int y) {
vx = x;
vy = y;
}
// update position according to velocity
public void updatePosition() {
locx += vx;
locy += vy;
}
}
</PRE>
<!-- END CODE //-->
<P>The UFO reuses most of the code from the BitmapLoop, but it overrides update() to provide alienlike behaviors. The new update() implements a state machine that permits the alien to switch behavior at random moments. You saw a simple example of state machines in the DancingRect classes of Chapter 2, Using Objects for Animation; the UFO machine is just a bit more complex. By using state machines, you create a simple kind of machine intelligence in your enemies.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">The Four UFO Behavioral States</FONT></H4>
<P>The UFO has four behaviors, each represented by one of the following states :
</P>
<DL>
<DD><B>•</B> Standby. When the UFO is in Standby mode, it moves back and forth horizontally.
<DD><B>•</B> Attack. An attacking UFO moves quickly downward, toward the missile launcher, and it is invulnerable to your missiles.
<DD><B>•</B> Retreat. The UFO can break off the attack at any moment, and retreat, which means that it moves up, toward the top of the screen.
<DD><B>•</B> Land. Finally, the alien can try to land, and to do this it descends vertically at a slow rate.
</DL>
<P>Figure 5-13 illustrates these various UFO behaviors.
</P>
<P><A NAME="Fig13"></A><A HREF="javascript:displayWindow('images/05-13.jpg',562,382 )"><IMG SRC="images/05-13t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/05-13.jpg',562,382)"><FONT COLOR="#000077"><B>Figure 5-13</B></FONT></A> UFO behaviors</P>
<P>Now let’s describe how the UFO can make transitions from state to state.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="171-177.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="179-185.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -