📄 111-116.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:Animating Sprites</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=3//-->
<!--PAGES=111-116//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="107-111.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch04/117-123.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 3-8</B> BouncingBitmap class</P>
<!-- CODE //-->
<PRE>
class BouncingBitmap extends BitmapSprite implements Moveable {
// the coords at which
// the bitmap bounces
protected int max_width;
protected int max_height;
// sprite velocity. used to implement Moveable interface
protected int vx;
protected int vy;
public BouncingBitmap(int x,int y,Image i,Applet a,
int max_w,int max_h) {
super(x,y,i,a);
max_width = max_w;
max_height = max_h;
}
public void setPosition(int x,int y) {
locx = x;
locy = y;
}
public void setVelocity(int x,int y) {
vx = x;
vy = y;
}
// update position according to velocity
public void updatePosition() {
locx += vx;
locy += vy;
}
// move and bounce bitmap if it hits borders
public void update() {
// flip x velocity if it hits left or right bound
if ((locx + width > max_width) ||
locx < 0) {
vx = -vx;
}
// flip y velocity if it hits top or bottom bound
if ((locy + height > max_height) ||
locy < 0) {
vy = -vy;
}
updatePosition();
}
}
</PRE>
<!-- END CODE //-->
<P>The implementation of the Moveable interface and the update() method are identical to those in BouncingRect. Thus, a BouncingBitmap behaves just like a BouncingRect, except it paints a bitmap! Figure 3-7 shows a Sprite hierarchy of everything we’ve constructed in this chapter.
</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/03-07.jpg',465,397 )"><IMG SRC="images/03-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-07.jpg',465,397)"><FONT COLOR="#000077"><B>Figure 3-7</B></FONT></A> Sprite class hierarchy</P>
<H3><A NAME="Heading26"></A><FONT COLOR="#000077">Using Sound</FONT></H3>
<P>Sound is an integral part of a full multimedia experience, and Java makes it easy for you to put sounds into your applets. Java represents sounds with AudioClip objects. AudioClip is an interface that’s defined in the java.applet package. There are two steps involved in using an AudioClip: loading an AudioClip, and playing the AudioClip.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading27"></A><FONT COLOR="#000077">Loading an AudioClip</FONT></H4>
<P>To load an AudioClip, use the getAudioClip() method, defined in the java.applet.Applet class. Here’s an example of the three ways you can use this method, which are analogous to the ways of loading an Image:
</P>
<!-- CODE //-->
<PRE>
// need this at the start of the code
import java.applet.*;
...
// Use the absolute URL of the sound file
AudioClip a =
getAudioClip(
new URL("http://www.somewhere.com/sounds/sound.au"));
// Find the sound file relative to the location of
// the HTML document that contains the applet
AudioClip b = getAudioClip(getDocumentBase(),"sound.au");
// Find the sound file relative to the location of
// the applet
AudioClip c = getAudioClip(getCodeBase(),"sound.au");
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading28"></A><FONT COLOR="#000077">Playing the Sound</FONT></H4>
<P>The interface java.applet.AudioClip defines the methods that an AudioClip object must have. They are
</P>
<DL>
<DD><B>•</B> play(). This method plays the sound.
<DD><B>•</B> loop(). This method loops the sound over and over.
<DD><B>•</B> stop(). Use this method to stop the sound.
</DL>
<P>For example:
</P>
<!-- CODE SNIP //-->
<PRE>
AudioClip a = ... // load AudioClip
a.play(); // play the sound
a.loop(); //loop the sound
a.stop(); // stop the sound
</PRE>
<!-- END CODE SNIP //-->
<P>Let’s use sound along with our new classes in the last applet of this chapter.
</P>
<H3><A NAME="Heading29"></A><FONT COLOR="#000077">Four Rectangles and a Sushi Chef</FONT></H3>
<P>This applet (shown in Figure 3-8) is a straightforward extension of what we have already built. By using the abstract Sprite class, you can plug a sprite in with a completely new representation, such as a bitmap, without making any changes to the animation driver. By creating an environment of plug-n-play sprites, you make game and graphics programming more understandable and extensible.
</P>
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/03-08.jpg',238,314 )"><IMG SRC="images/03-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-08.jpg',238,314)"><FONT COLOR="#000077"><B>Figure 3-8</B></FONT></A> Four rectangles and a sushi chef</P>
<P>In fact, Listing 3-9 shows the only change from Listing 3-6.
</P>
<P><B>Listing 3-9</B> New initSprites() method</P>
<!-- CODE //-->
<PRE>
public void initSprites() {
sprites = new Sprite[NUM_SPRITES]; // init sprite array
sprites[0] = new BouncingBitmap(37,37,
getImage(getCodeBase(),
"sushi.gif"),
this,
width-1,height-1);
sprites[1] = new BouncingRect(0,0,30,30,Color.yellow,
width-1,height-1);
sprites[2] = new BouncingRect(17,17,13,13,Color.red,
width-1,height-1);
// border of the smaller box
sprites[3] = new RectSprite(0,0,114,114,Color.green);
// this rect bounces in a smaller box!
sprites[4] = new BouncingRect(13,13,17,17,Color.green,
114,114);
// define sprite for border
sprites[5] = new RectSprite(0,0,width-1,height-1,Color.green);
((Moveable)sprites[1]).setVelocity(4,3);
((Moveable)sprites[2]).setVelocity(1,2);
((Moveable)sprites[4]).setVelocity(3,1);
((Sprite2D)sprites[4]).setFill(true); // fill this sprite
((Moveable)sprites[0]).setVelocity(1,3);
((BitmapSprite)sprites[0]).setSize(144,113);
}
</PRE>
<!-- END CODE //-->
<P>As you see, the sushi bitmap is defined as sprite 0, which is the lowest priority, so it will appear behind everything else. Put this method into the Bounce applet, and be sure to redefine
</P>
<!-- CODE SNIP //-->
<PRE>
static final int NUM_SPRITES = 6;
</PRE>
<!-- END CODE SNIP //-->
<P>so the applet knows how many sprites there are!
</P>
<P>A final point. Notice how the abstraction provided by the Moveable interface allows you to request behavior from relatively distant classes in a clear, uniform manner. This is illustrated by the last six lines of initSprites(). You’ll be using more interfaces in future chapters.</P>
<H3><A NAME="Heading30"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<P>Here are three ideas that you should think about (for fun!). We will cover the solutions in the following chapters.
</P>
<DL>
<DD><B>•</B> Create Sprite2D subclasses for the other primitives in java.awt.Graphics. Follow the model of the RectSprite to make an ArcSprite, OvalSprite, or PolygonSprite.
<DD><B>•</B> Right now, the BitmapSprite only displays one bitmap. Subclass a BitmapLoop, which will display a sequence of bitmaps in a single sprite. You will want to define an array of Images to do this, and a counter that tracks the current frame.
<DD><B>•</B> In the bouncing applets, the sprites don’t interact with each other. Consider creating an interface to allow sprites to determine if an intersection has occurred. Then you can create an animation where all sprites bounce off each other!
</DL>
<H3><A NAME="Heading31"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>In this chapter, you’ve learned about abstract classes and interfaces, and how they permit the clean, modular design of applets. In particular, you’ve created Sprite classes that you’ll use in your first Java game (which is coming up really soon!). And you now know how to use bitmaps, which are important in creating customized looks for your graphics applets.
</P>
<P>Next, you will see how to take control of your sprites!</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="107-111.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch04/117-123.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -