📄 458-461.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 3D Applets with App3Dcore</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=12//-->
<!--PAGES=458-461//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="455-458.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="462-465.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading22"></A><FONT COLOR="#000077">Using a Template to Simplify Designing New Objects</FONT></H4>
<P>To make the designing of new objects easier, I have put together a template, shown in Listing 12-7, with the basic methods that can be overridden.
</P>
<P><B>Listing 12-7</B> The template</P>
<!-- CODE //-->
<PRE>
class MyCoolObject extends fObject{
/**
* Constructor.
*/
MyCoolObject(fWorld world){
super(world,new fPoint3d(0,0,0),new fAngle3d(0,0,0));
usePolyhedronInstance(new
fPolyhedronInstance(ourDefaultPolyhedron,ourScale));
//--
//-- insert code here
//--
}
/**
* Updates this object by dt seconds.
*/
public void update (double dt) {
super.update(dt);
//--
//-- insert code here
//--
}
/**
* The core will ask this object if it is interested of
* collision with some other object. Return true if the object
* is interested otherwise let the base class decide.
*/
public boolean interestedOfCollisionWith (fObject obj) {
//--
//-- insert code here, example
//-- if(obj instanceof MyCoolClass) return true;
//--
return super.interestedOfCollisionWith(obj);
}
/**
* Handles a collision with a object. Returns false if there
* is no point in checking more collisions. I.e. the object is dead.
*/
protected boolean handleCollisionWith (fObject obj,double dt) {
//--
//-- insert code here, example
//-- if(obj instanceof AtomicBomb) {die();return false;}
//--
return super.handleCollisionWith(obj,dt);
}
/**
* Kills this object.
*/
protected void die () {
super.die();
//--
//-- insert code here, example
//-- largeExplosion();
}
/**
* Inititates this class by loading the static parameters from
* the applet.
*/
public static void initiateClass (Applet app) {
try{
//-- load the default polyhedron for all bouncing boxes
//-- this MUST be done in all non-abstract classes
String polyfile=
app.getParameter("MyCoolObject_ourDefaultPolyhedron");
URL url=new URL(app.getCodeBase(),polyfile);
InputStream is=url.openStream();
ourDefaultPolyhedron=new fConvexPolyhedron(is);
} catch(Exception e) {
e.printStackTrace();
}
//-- the scaling of the polyhedron
double xscale=new
Double(app.getParameter("MyCoolObject_scalex")).doubleValue();
double yscale=new
Double(app.getParameter("MyCoolObject_scaley")).doubleValue();
double zscale=new
Double(app.getParameter("MyCoolObject_scalez")).doubleValue();
ourScale=new fPoint3d(xscale,yscale,zscale);
//--
//-- load your other constants here
//--
}
//-- class "constants"
protected static fPolyhedron ourDefaultPolyhedron;
protected static fPoint3d ourScale;
//--
//-- insert your static stuff here
//--
}
</PRE>
<!-- END CODE //-->
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Things to Watch out For</B></FONT>
<DL>
<DD><B>1.</B> When overriding a method, copy it from the template or you might misspell it.
<DD><B>2.</B> Always be sure to initiate all classes in yourWorld.initiateClasses().
<DD><B>3.</B> Always make sure to add the new parameters to the HTML file when you create a new class.
<DD><B>4.</B> Make sure the parameters are loaded properly and that they are not misspelled, or they’ll be null or zero and possibly crash the core.
<DD><B>5.</B> When using getPosition(), getAngle(), getdPosition(), or getdAngle(), you will receive a clone of that object. You have to call setPosition(), etc., in order to really change the state of the object; otherwise you will only change the clone.
<DD><B>6.</B> Do not forget to call the method usePolyhedronInstance(..) in all nonabstract classes or the object will not have a 3D model.
<DD><B>7.</B> Be sure you have checked points 1, 2, 3, 4, 5, 6, and 7.
</DL>
</TABLE>
<H3><A NAME="Heading23"></A><FONT COLOR="#000077">Creating the Game Layer</FONT></H3>
<P>In this section, a layer of classes called the game layer will be developed. This layer of classes will turn the general App3Dcore into a game engine. This layer also breaks the intimate contact with the core. Figure 12-5 shows an abstract representation of how the classes are extended layer by layer.
</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/12-05.jpg',594,405 )"><IMG SRC="images/12-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/12-05.jpg',594,405)"><FONT COLOR="#000077"><B>Figure 12-5</B></FONT></A> Extending the App3Dcore<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="455-458.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="462-465.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -