📄 543-547.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 the JAVAroids 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=13//-->
<!--PAGES=543-547//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="539-543.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="547-561.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">The Explosion Sprite</FONT></H4>
<P>The Explosion sprite is actually a series of lines that blows apart. Each of these lines will be a distinct Sprite that the Explosion will contain. Since the concept of a container sprite is so useful, let’s define it, in Listing 13-12.
</P>
<P><B>Listing 13-12</B> ContainerSprite class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// ContainerSprite: Container class for sprites
//
/////////////////////////////////////////////////////////////////
public class ContainerSprite extends Sprite {
// vector of the constituent sprites
protected Vector sprites;
/////////////////////////////////////////////////////////////////
// constructor
/////////////////////////////////////////////////////////////////
public ContainerSprite() {
sprites = new Vector();
}
/////////////////////////////////////////////////////////////////
// add a new sprite
/////////////////////////////////////////////////////////////////
public void addSprite(Sprite s) {
sprites.addElement(s);
}
/////////////////////////////////////////////////////////////////
// paint each constituent sprite in sprites vector
/////////////////////////////////////////////////////////////////
public void paint(Graphics g) {
if (isVisible()) {
Enumeration e;
e = sprites.elements();
while (e.hasMoreElements()) {
Sprite s = (Sprite) e.nextElement();
s.paint(g);
}
}
}
/////////////////////////////////////////////////////////////////
// update each constituent sprite in sprites vector
/////////////////////////////////////////////////////////////////
public void update() {
if (isActive()) {
Enumeration e;
e = sprites.elements();
while (e.hasMoreElements()) {
Sprite s = (Sprite) e.nextElement();
s.update();
}
}
}
}
</PRE>
<!-- END CODE //-->
<P>As you see, the sprites in the container are stored in a Vector. The paint() and update() methods iterate through the constituent sprites, painting and updating each one individually.
</P>
<P>An Explosion is a ContainerSprite that animates a collection of MoveablePolygons. In this implementation of Explosion, the constituent polygons are straight lines (though you can easily define your own shape). Each time an Explosion is updated, it moves the subsprites farther outward, like shock waves from the center of an explosion. When the Explosion updates a certain number of times, it sets the <I>done</I> flag, which will be used by the effect manager.</P>
<P>Figure 13-16 illustrates the action of the Explosion sprite, and Listing 13-13 shows the implementation of this class.</P>
<P><A NAME="Fig16"></A><A HREF="javascript:displayWindow('images/13-16.jpg',600,409 )"><IMG SRC="images/13-16t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/13-16.jpg',600,409)"><FONT COLOR="#000077"><B>Figure 13-16</B></FONT></A> Explosion sprite</P>
<P><B>Listing 13-13</B> Explosion class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// Explosion sprite class: contains lines that blow apart
//
/////////////////////////////////////////////////////////////////
public class Explosion extends ContainerSprite {
// center of explosion
protected int locx,locy;
// size of lines of explosion
static final int EXP_SIZE = 13;
// how long explosion lasts
protected int max_updates = 0;
// updates so far
protected int updates;
// the constituent lines in the container
static final int ex[][] = { {0,0} , {-EXP_SIZE,EXP_SIZE},
{0,0}, {-EXP_SIZE,EXP_SIZE}};
static final int ey[][] = { {-EXP_SIZE,EXP_SIZE}, {0,0},
{-EXP_SIZE,EXP_SIZE}, {0,0} };
// the constituent edge sprites in container
MoveablePolygon edges[] = new MoveablePolygon[ex.length];
// size of explosion
protected float size;
// is explosion over?
protected boolean done = false;
/////////////////////////////////////////////////////////////////
// Explosion constructor: color, position, #updates
/////////////////////////////////////////////////////////////////
public Explosion(Color color, int x, int y, int u) {
// define edge sprites of explosion
for (int i=0; i<edges.length; i++) {
edges[i] = new MoveablePolygon(ex[i],ey[i],ex[i].length,
x,y,color);
edges[i].restore(); // show all edges
// add each edge to container
addSprite(edges[i]);
}
// initialize how long the explosion lasts
max_updates = u;
// initialize counter
updates = 0;
// set explosion at the given coordinates
locx = x;
locy = y;
// restore explosion sprite
restore();
// set size
size = 1.0f;
// reset boolean
done = false;
}
/////////////////////////////////////////////////////////////////
// update explosion or suspend if it reaches maximum size
// (assumes 4 edges in the container, which move apart
// along x,y coordinates.)
/////////////////////////////////////////////////////////////////
public void update() {
if (updates++ < max_updates) {
// blow the lines apart!
size *= 1.7f; // increase size
int isize = (int)size; // cast size to int
edges[0].setPosition(locx + isize,locy );
edges[1].setPosition(locx ,locy + isize);
edges[2].setPosition(locx - isize,locy);
edges[3].setPosition(locx,locy - isize);
}
else
done = true;
}
/////////////////////////////////////////////////////////////////
// return explosion status
/////////////////////////////////////////////////////////////////
public boolean isDone() {
return done;
}
}
</PRE>
<!-- END CODE //-->
<P>Now it’s time to define the manager classes!
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="539-543.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="547-561.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -