📄 090-093.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=090-093//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="087-089.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="093-097.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Defining the Sprite Class</FONT></H3>
<P>Let’s define the Sprite class that will be at the top of the Sprite hierarchy. To do this, let’s recap the essential features of our sprites:
</P>
<DL>
<DD><B>•</B> State: internal representation of onscreen appearance, location on screen, visibility, priority, updateability
<DD><B>•</B> Behavior: painting, updating
</DL>
<P>The root of the Sprite hierarchy should specify and implement as many of these elements as possible, to promote a common interface and functionality among all sprites. However, most of the implementation will be deferred to the subclasses. For example, the sprites we will define have a variety of internal representations (some of which we don’t even know yet), and it makes sense to leave their implementation to subclasses. Behaviors such as painting and updating rely on the internal representation of the sprite, and they’ll also be given concrete form by the appropriate subclass. Thus, the paint() and update() methods, and the Sprite class itself, will be declared abstract.
</P>
<P>The definition of Sprite is shown in Listing 3-1.</P>
<P><B>Listing 3-1</B> Sprite class</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);
}
}
</PRE>
<!-- END CODE //-->
<P>Let’s examine this class. The booleans <I>visible</I> and <I>active</I> keep track of whether the sprite can be seen and updated. The notions of suspending a sprite (resetting both <I>visible</I> and <I>active</I>) and restoring a sprite (setting both booleans) are so common that they are implemented as distinct methods. Finally, the paint() and update() methods are declared abstract, as we have discussed earlier, since they depend on how the appearance of the sprite is represented internally.</P>
<P>You might be wondering what the <I>protected</I> keyword (in front of the boolean declarations) refers to. This is an example of an <I>access specifier</I>, and since these come up rather often, let’s discuss what they are.</P>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Using Access Specifiers</FONT></H3>
<P>As you know, one of the key features of objects is <I>encapsulation</I>, which means that an object’s variables and methods are bundled inside it, and shielded from the outside world. Encapsulation makes building complex software easier, because it limits the interdependencies between various sections of code. The degree of encapsulation can be modified by <I>access specifiers—private, public, and protected</I>—which allow you to specify the access level allowed. Java supports four levels of access:</P>
<DL>
<DD><B>•</B> Public access
<DD><B>•</B> Private access
<DD><B>•</B> Protected access
<DD><B>•</B> Package/default access
</DL>
<P>Now let’s find out what these mean. Consider the following class definition:
</P>
<!-- CODE //-->
<PRE>
public class Foo {
public float publicNumber = 13.17f;
public void publicMethod() {
...
}
private double privateNumber = -4.4;
private int privateMethod() {
...
}
protected float protectedNumber = 17.13f;
protected void protectedMethod() {
...
}
int defaultAccessLevelNumber;
void defaultAccessLevelMethod() {
...
}
}
</PRE>
<!-- END CODE //-->
<P>Let’s discuss the four different access levels that are present in class Foo.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Public Access</FONT></H4>
<P>The public access level is the most liberal level in Java. Variables or methods declared public are accessible to arbitrary classes. Furthermore, the public variables and methods of a class are inherited by its subclasses.
</P>
<P>Here’s an example of public access. Any class that instantiates a Foo object f can modify its public variables and call its public methods. The following code could appear in the method of any class:</P>
<!-- CODE SNIP //-->
<PRE>
f.publicNumber = 4.4f + 4.9f; // access allowed
f.publicMethod(); // access allowed
</PRE>
<!-- END CODE SNIP //-->
<P>A class uses public methods to allow arbitrary clients to access its functionality. On the other hand, you should be really careful when using public variables. Any object can change the value of a public variable, and computations that depend on this value will change as well. This can lead to code that’s bug prone and hard to understand. Thus, most instance variables should not be public unless absolutely necessary.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Private Access</FONT></H4>
<P>The private access level stands at the opposite end of the spectrum from public access. It is the most restrictive level. Unlike public members, private variables or methods are <I>only</I> accessible within the class itself.</P>
<P>Thus, if another class (even a subclass) tried the following, the compiler would not accept it.</P>
<!-- CODE SNIP //-->
<PRE>
// f is a Foo object
f.privateNumber = 7.3 - 4.4; // access NOT allowed
f.privateMethod(); // access NOT allowed
</PRE>
<!-- END CODE SNIP //-->
<P>Use private methods as often as necessary in your games. First of all, they don’t incur the performance penalty associated with dynamic method binding, so they’re slightly more efficient than regular instance methods. Furthermore, they hide the implementation of the class, which eliminates the possibility of another class calling a method it’s not supposed to.
</P>
<P>Private variables keep external objects from accidentally or malignantly modifying the object’s state, so they’re “safer” to use than public variables.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="087-089.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="093-097.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -