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

📄 543-547.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: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,&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=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&#146;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 &#123;
  // vector of the constituent sprites
  protected Vector sprites;

/////////////////////////////////////////////////////////////////
// constructor
/////////////////////////////////////////////////////////////////

  public ContainerSprite() &#123;
    sprites = new Vector();
  &#125;

/////////////////////////////////////////////////////////////////
// add a new sprite
/////////////////////////////////////////////////////////////////

  public void addSprite(Sprite s) &#123;
    sprites.addElement(s);
  &#125;

/////////////////////////////////////////////////////////////////
// paint each constituent sprite in sprites vector
/////////////////////////////////////////////////////////////////

  public void paint(Graphics g) &#123;
    if (isVisible()) &#123;
      Enumeration e;
      e = sprites.elements();
      while (e.hasMoreElements()) &#123;
       Sprite s = (Sprite) e.nextElement();
       s.paint(g);
      &#125;
    &#125;
  &#125;

/////////////////////////////////////////////////////////////////
// update each constituent sprite in sprites vector
/////////////////////////////////////////////////////////////////

  public void update() &#123;
    if (isActive()) &#123;
      Enumeration e;
      e = sprites.elements();
      while (e.hasMoreElements()) &#123;
       Sprite s = (Sprite) e.nextElement();
       s.update();
      &#125;
    &#125;
  &#125;
&#125;
</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>&nbsp;&nbsp;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 &#123;
  // 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[][] = &#123; &#123;0,0&#125; , &#123;-EXP_SIZE,EXP_SIZE&#125;,
                            &#123;0,0&#125;, &#123;-EXP_SIZE,EXP_SIZE&#125;&#125;;
  static final int ey[][] = &#123; &#123;-EXP_SIZE,EXP_SIZE&#125;, &#123;0,0&#125;,
                            &#123;-EXP_SIZE,EXP_SIZE&#125;, &#123;0,0&#125; &#125;;

  // 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) &#123;

    // define edge sprites of explosion

    for (int i=0; i&lt;edges.length; i&#43;&#43;) &#123;
      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]);
    &#125;

    // 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;
  &#125;

/////////////////////////////////////////////////////////////////
// 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() &#123;
    if (updates&#43;&#43; &lt; max_updates) &#123;
      // blow the lines apart!
      size *= 1.7f;               // increase size

      int isize = (int)size;      // cast size to int
      edges[0].setPosition(locx &#43; isize,locy );
      edges[1].setPosition(locx ,locy &#43; isize);
      edges[2].setPosition(locx - isize,locy);
      edges[3].setPosition(locx,locy - isize);

    &#125;
    else
      done = true;

  &#125;

/////////////////////////////////////////////////////////////////
// return explosion status
/////////////////////////////////////////////////////////////////

  public boolean isDone() &#123;
    return done;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>Now it&#146;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 + -