📄 161-167.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=161-167//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="153-161.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="167-171.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Defining the GunManager</FONT></H3>
<P>The GunManager communicates with two sprites: one that draws the missile gun, another that paints the missile that has been fired. Let’s define these sprites before proceeding to the GunManager. Both the missile and the launcher will derive from the sprite classes that you have created in the previous chapters. Listing 5-2 shows the definitions of Sprite and Sprite2D, which are the two abstract classes that rest at the top of the sprite hierarchy.
</P>
<P><B>Listing 5-2</B> Sprite and Sprite2D classes</P>
<!-- CODE //-->
<PRE>
abstract class Sprite {
protected boolean visible; // is sprite visible
protected boolean active; // is sprite updateable
// abstract methods:
abstract void paint (Graphics g);
abstract void update();
// accessor methods:
public boolean isVisible() {
return visible;
}
public void setVisible(boolean b) {
visible = b;
}
public boolean isActive() {
return active;
}
public void setActive(boolean b) {
active = b;
}
// suspend the sprite
public void suspend() {
setVisible(false);
setActive(false);
}
// restore the sprite
public void restore() {
setVisible(true);
setActive(true);
}
}
abstract class Sprite2D extends Sprite {
protected int locx;
protected int locy;
Color color;
boolean fill;
public boolean getFill() {
return fill;
}
public void setFill(boolean b) {
fill = b;
}
public void setColor(Color c) {
color = c;
}
public Color getColor() {
return color;
}
}
</PRE>
<!-- END CODE //-->
<P>Now let’s define the GunSprite class.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">GunSprite</FONT></H4>
<P>The GunSprite keeps all information that relates to the appearance of the missile gun onscreen, such as a bitmap for the actual gun (shown in Figure 5-1) and its coordinates in the applet.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">The BitmapSprite Class</FONT></H4>
<P>Let’s subclass GunSprite from the BitmapSprite class we created back in Chapter 3, Animating Sprites. The BitmapSprite class is shown in Listing 5-3, with a new constructor added for convenience.
</P>
<P><B>Listing 5-3</B> BitmapSprite class</P>
<!-- CODE //-->
<PRE>
class BitmapSprite extends Sprite {
protected int locx;
protected int locy;
// image dimensions
protected int width,height;
protected Image image; // the bitmap
protected Applet applet; // the parent applet
public BitmapSprite(Image i,Applet a) {
locx = 0;
locy = 0;
image = i;
applet = a;
if (image != null) {
width = image.getWidth(a); // get size of background
height = image.getHeight(a);
}
restore();
}
public BitmapSprite(int x,int y,Image i,Applet a) {
locx = x;
locy = y;
image = i;
applet = a;
if (image != null) {
width = image.getWidth(a); // get size of background
height = image.getHeight(a);
}
restore();
}
public void setSize(int w,int h) {
width = w;
height = h;
}
public void update() {
// do nothing
}
public void paint(Graphics g) {
if (visible) {
g.drawImage(image,locx,locy,applet);
}
}
}
</PRE>
<!-- END CODE //-->
<P>GunSprite will inherit from BitmapSprite, but it also needs other public methods that permit it to interact with the outside world. By specifying these methods in interfaces, you can formalize the interactions they represent and apply them to other objects. The Moveable interface is an example of such an interface.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">The Moveable Interface</FONT></H4>
<P>The Moveable interface, which we initially defined in Chapter 3, Animating Sprites, will enable external objects such as the GunManager to tell GunSprite where to move. Listing 5-4 defines the Moveable interface.
</P>
<P><B>Listing 5-4</B> Moveable interface</P>
<!-- CODE SNIP //-->
<PRE>
interface Moveable {
public abstract void setPosition(int x, int y);
public abstract void setVelocity(int x, int y);
public abstract void updatePosition();
}
</PRE>
<!-- END CODE SNIP //-->
<P>The GunSprite class will implement Moveable.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">The Intersect Interface</FONT></H4>
<P>Another interface you’ll need is the Intersect interface, which allows sprites to ask each other if an intersection has occurred. Think of this interface as formalizing the interaction that occurs between a missile sprite and its target, as shown in Figure 5-9.
</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/05-09.jpg',561,382 )"><IMG SRC="images/05-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/05-09.jpg',561,382)"><FONT COLOR="#000077"><B>Figure 5-9</B></FONT></A> Interaction between missile and target</P>
<P>The missile object passes its position and area on the screen to the target sprite. The target object performs the intersection test and replies <I>true</I> or <I>false</I> depending on the result.</P>
<P>Finally, the missile sprite can notify the target of the collision. The definition of the Intersect interface is shown in Listing 5-5.</P>
<P><B>Listing 5-5</B> Intersect interface</P>
<!-- CODE SNIP //-->
<PRE>
interface Intersect {
public boolean intersect(int x1,int y1,int x2,int y2);
public void hit();
}
</PRE>
<!-- END CODE SNIP //-->
<P>The GunSprite will implement Intersect, since aliens need to know if a collision has occurred with the missile launcher. One question you might be having—how do you decide if two sprites collide?
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="153-161.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="167-171.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -