亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch6.htm

?? 21天學會用java開發網絡游戲 這對于 想學java編程的人會很有用
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
&nbsp;&nbsp;&nbsp;&nbsp;// Get a random position<BR>
&nbsp;&nbsp;&nbsp;&nbsp;pos.x = Math.abs(rand.nextInt() %<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;background.getSize().width);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;pos.y = Math.abs(rand.nextInt() %<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;background.getSize().height);
<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Iterate through sprites, checking
if position is empty<BR>
&nbsp;&nbsp;&nbsp;&nbsp;boolean collision = false;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; size(); i++) {
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rectangle testPos = ((Sprite)elementAt(i)).getPosition();
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (pos.intersects(testPos))
{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collision = true;
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;empty = !collision;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;return new Point(pos.x, pos.y);</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
<TT><FONT FACE="Courier">getEmptyPosition</FONT></TT> is a method
whose importance might not be readily apparent to you right now;
it is used to find an empty physical position in which to place
a new sprite in the sprite list. This doesn't mean the position
of the sprite in the list; rather, it means its physical position
on the screen. This method is very useful when you want to randomly
place multiple sprites on the screen. By using <TT><FONT FACE="Courier">getEmptyPosition</FONT></TT>,
you eliminate the possibility of placing new sprites on top of
existing sprites. For example, in an adventure game you could
randomly place scenery objects such as trees using <TT><FONT FACE="Courier">getEmptyPosition</FONT></TT>
to make sure none of them overlap each other.
<P>
The <TT><FONT FACE="Courier">isPointInside</FONT></TT> method
in <TT><FONT FACE="Courier">SpriteVector</FONT></TT> is similar
to the version of <TT><FONT FACE="Courier">isPointInside</FONT></TT>
in <TT><FONT FACE="Courier">Sprite</FONT></TT>, except it goes
through the entire sprite list checking each sprite. Check out
the source code for it:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Sprite isPointInside(Point pt) {<BR>
&nbsp;&nbsp;// Iterate backward through the sprites, testing each
<BR>
&nbsp;&nbsp;for (int i = (size() - 1); i &gt;= 0; i--) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;Sprite s = (Sprite)elementAt(i);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (s.isPointInside(pt))<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;return null;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
If the point passed in the parameter <TT><FONT FACE="Courier">pt</FONT></TT>
lies in a sprite, <TT><FONT FACE="Courier">isPointInside</FONT></TT>
returns the sprite. Notice that the sprite list is searched in
reverse, meaning that the last sprite is checked before the first.
The sprites are searched in this order for a very important reason:
Z-order. The sprites are stored in the sprite list sorted in ascending
Z-order, which specifies their depth on the screen. Therefore,
sprites near the beginning of the list are sometimes concealed
by sprites near the end of the list. If you want to check for
a point lying within a sprite, it only makes sense to check the
topmost sprites first-that is, the sprites with larger Z-order
values. If this sounds a little confusing, don't worry; you'll
learn more about Z-order later today when you get to the <TT><FONT FACE="Courier">add</FONT></TT>
method.
<P>
As in <TT><FONT FACE="Courier">Sprite</FONT></TT>, the <TT><FONT FACE="Courier">update</FONT></TT>
method is the key method in <TT><FONT FACE="Courier">SpriteVector</FONT></TT>
because it handles updating all the sprites. Listing 6.3 contains
the source code for <TT><FONT FACE="Courier">update</FONT></TT>.
<HR>
<BLOCKQUOTE>
<B>Listing 6.3. The </B><TT><B><FONT FACE="Courier">SpriteVector</FONT></B></TT><B>
class's </B><TT><B><FONT FACE="Courier">update</FONT></B></TT><B>
method.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public void update() {<BR>
&nbsp;&nbsp;// Iterate through sprites, updating each<BR>
&nbsp;&nbsp;Sprite&nbsp;&nbsp;&nbsp;&nbsp;s, sHit;<BR>
&nbsp;&nbsp;Rectangle lastPos;<BR>
&nbsp;&nbsp;for (int i = 0; i &lt; size(); ) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Update the sprite<BR>
&nbsp;&nbsp;&nbsp;&nbsp;s = (Sprite)elementAt(i);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;lastPos = new Rectangle(s.getPosition().x,
s.getPosition().y,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.getPosition().width, s.getPosition().height);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;BitSet action = s.update();<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Check for the SA_ADDSPRITE action<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (action.get(Sprite.SA_ADDSPRITE)) {
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Add the sprite<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sprite sToAdd = s.addSprite(action);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (sToAdd != null) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int iAdd = add(sToAdd);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (iAdd &gt;=
0 &amp;&amp; iAdd &lt;= i)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Check for the SA_RESTOREPOS action
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (action.get(Sprite.SA_RESTOREPOS))
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.setPosition(lastPos);<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Check for the SA_KILL action<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (action.get(Sprite.SA_KILL)) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;removeElementAt(i);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// Test for collision<BR>
&nbsp;&nbsp;&nbsp;&nbsp;int iHit = testCollision(s);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (iHit &gt;= 0)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (collision(i, iHit))<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.setPosition(lastPos);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;i++;<BR>
&nbsp;&nbsp;}</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The <TT><FONT FACE="Courier">update</FONT></TT> method iterates
through the sprites, calling <TT><FONT FACE="Courier">Sprite</FONT></TT>'s
<TT><FONT FACE="Courier">update</FONT></TT> method on each one.
It then checks for the various sprite action flags returned by
the call to <TT><FONT FACE="Courier">update</FONT></TT>. If the
<TT><FONT FACE="Courier">SA_ADDSPRITE</FONT></TT> flag is set,
the <TT><FONT FACE="Courier">addSprite</FONT></TT> method is called
on the sprite and the returned sprite is added to the list. If
the <TT><FONT FACE="Courier">SA_RESTOREPOS</FONT></TT> flag is
set, the sprite position is set to the position of the sprite
prior to being updated. If the <TT><FONT FACE="Courier">SA_KILL</FONT></TT>
flag is set, the sprite is removed from the sprite list. Finally,
<TT><FONT FACE="Courier">testCollision</FONT></TT> is called to
see whether a collision has occurred between sprites. You get
the whole scoop on <TT><FONT FACE="Courier">testCollision</FONT></TT>
in this section. If a collision has occurred, the old position
of the collided sprite is restored and the <TT><FONT FACE="Courier">collision</FONT></TT>
method is called.
<P>
The <TT><FONT FACE="Courier">collision</FONT></TT> method is used
to handle collisions between two sprites:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">protected boolean collision(int i, int
iHit) {<BR>
&nbsp;&nbsp;// Swap velocities (bounce)<BR>
&nbsp;&nbsp;Sprite s = (Sprite)elementAt(i);<BR>
&nbsp;&nbsp;Sprite sHit = (Sprite)elementAt(iHit);<BR>
&nbsp;&nbsp;Point swap = s.getVelocity();<BR>
&nbsp;&nbsp;s.setVelocity(sHit.getVelocity());<BR>
&nbsp;&nbsp;sHit.setVelocity(swap);<BR>
&nbsp;&nbsp;return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">collision</FONT></TT> method is responsible
for handling any actions that result from a collision between
sprites. The action in this case is to simply swap the velocities
of the collided <TT><FONT FACE="Courier">Sprite</FONT></TT> objects,
which results in a bouncing effect. This method is where you provide
specific collision actions in derived sprites. For example, in
a space game, you might want alien sprites to explode upon collision
with a meteor sprite.
<P>
The <TT><FONT FACE="Courier">testCollision</FONT></TT> method
is used to test for collisions between a sprite and the rest of
the sprites in the sprite list:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">protected int testCollision(Sprite test)
{<BR>
&nbsp;&nbsp;// Check for collision with other sprites<BR>
&nbsp;&nbsp;Sprite&nbsp;&nbsp;s;<BR>
&nbsp;&nbsp;for (int i = 0; i &lt; size(); i++)<BR>
&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;s = (Sprite)elementAt(i);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (s == test)&nbsp;&nbsp;// don't check
itself<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (test.testCollision(s))<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return i;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;return -1;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
The sprite to be tested is passed in the <TT><FONT FACE="Courier">test</FONT></TT>
parameter. The sprites are then iterated through and the <TT><FONT FACE="Courier">testCollision</FONT></TT>
method in <TT><FONT FACE="Courier">Sprite</FONT></TT> is called
for each. Notice that <TT><FONT FACE="Courier">testCollision</FONT></TT>
isn't called on the test sprite if the iteration refers to the
same sprite. To understand the significance of this code, consider
the effect of passing <TT><FONT FACE="Courier">testCollision</FONT></TT>
the same sprite on which the method is being called; you would
be checking to see whether a sprite was colliding with itself,
which would always return true. If a collision is detected, the
<TT><FONT FACE="Courier">Sprite</FONT></TT> object that has been
hit is returned from <TT><FONT FACE="Courier">testCollision</FONT></TT>.
<P>
The <TT><FONT FACE="Courier">draw</FONT></TT> method handles drawing
the background, as well as drawing all the sprites:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public void draw(Graphics g) {<BR>
&nbsp;&nbsp;// Draw the background<BR>
&nbsp;&nbsp;background.draw(g);<BR>
<BR>
&nbsp;&nbsp;// Iterate through sprites, drawing each<BR>
&nbsp;&nbsp;for (int i = 0; i &lt; size(); i++)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;((Sprite)elementAt(i)).draw(g);<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
The background is drawn with a simple call to the <TT><FONT FACE="Courier">draw</FONT></TT>
method of the <TT><FONT FACE="Courier">Background</FONT></TT>
object. The sprites are then drawn by iterating through the sprite
list and calling the <TT><FONT FACE="Courier">draw</FONT></TT>
method for each.
<P>
The <TT><FONT FACE="Courier">add</FONT></TT> method is probably
the trickiest method in the <TT><FONT FACE="Courier">SpriteVector</FONT></TT>
class. Listing 6.4 contains the source code for <TT><FONT FACE="Courier">add</FONT></TT>.
<HR>
<BLOCKQUOTE>
<B>Listing 6.4. The </B><TT><B><FONT FACE="Courier">SpriteVector</FONT></B></TT><B>
class's </B><TT><B><FONT FACE="Courier">add</FONT></B></TT><B>
method.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public int add(Sprite s) {<BR>
&nbsp;&nbsp;// Use a binary search to find the right location
to insert the<BR>
&nbsp;&nbsp;// new sprite (based on z-order)<BR>
&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;l = 0, r = size(), i = 0;<BR>
&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;z = s.getZOrder(),<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zTest = z + 1;
<BR>
&nbsp;&nbsp;while (r &gt; l) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;i = (l + r) / 2;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;zTest = ((Sprite)elementAt(i)).

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
91精品国产综合久久婷婷香蕉| 日韩理论片网站| 在线日韩av片| 日韩欧美视频在线| 美日韩一级片在线观看| 日本网站在线观看一区二区三区 | 日韩中文字幕区一区有砖一区| 亚洲国产综合在线| 日本一区二区视频在线观看| 欧美精品乱人伦久久久久久| 91亚洲午夜精品久久久久久| 国产乱人伦偷精品视频免下载 | 欧美电影一区二区| 91福利在线导航| 色偷偷成人一区二区三区91| 99免费精品视频| 国产a久久麻豆| 国产高清无密码一区二区三区| 久久夜色精品一区| 国产精品自拍在线| 蜜桃视频在线一区| 亚洲精品在线网站| 日韩欧美区一区二| 欧美成人福利视频| 久久av资源网| 久久国产夜色精品鲁鲁99| 亚洲高清在线视频| 亚洲成人av免费| 亚洲国产精品人人做人人爽| 在线日韩国产精品| 777久久久精品| 日韩欧美的一区| av毛片久久久久**hd| 91亚洲精品乱码久久久久久蜜桃| 亚洲国产日韩a在线播放性色| 欧美精品在线观看播放| 欧美人体做爰大胆视频| 欧美日韩国产另类不卡| 日韩欧美一级二级| 中文字幕巨乱亚洲| 午夜伦欧美伦电影理论片| 日本中文字幕一区二区有限公司| 精品久久五月天| jlzzjlzz亚洲日本少妇| 午夜精品久久久久久久久| 免费在线观看一区二区三区| 亚洲欧美电影一区二区| 五月婷婷另类国产| 国产黑丝在线一区二区三区| 欧美日韩专区在线| 中文字幕在线免费不卡| 日韩精品亚洲一区二区三区免费| 国产91精品久久久久久久网曝门| 亚洲国产欧美一区二区三区丁香婷| 久久久久亚洲蜜桃| 亚洲一区免费视频| 国产精品一二三在| 在线成人av影院| 欧美韩国日本综合| 久久精品国产久精国产| 色狠狠色狠狠综合| 精品国产3级a| 日韩av一区二区三区四区| 99精品久久久久久| 久久精品一二三| 国产精品资源网站| 日韩精品一区二区三区视频在线观看| 884aa四虎影成人精品一区| 日韩一区在线看| 99久久国产综合精品女不卡| 国产成人一区二区精品非洲| 日韩一级片网站| 日本aⅴ亚洲精品中文乱码| 91精品国产综合久久久久久| 一区在线播放视频| 91色在线porny| 欧美三级日韩在线| 一区二区三区中文字幕| 99vv1com这只有精品| 亚洲欧美色一区| 日韩午夜激情视频| 亚洲永久免费av| 日韩一区二区三区四区五区六区| 精品日韩成人av| 亚洲欧美中日韩| 色中色一区二区| 亚洲大片在线观看| 欧美大片在线观看一区| 精品一区二区三区蜜桃| 成人自拍视频在线| 亚洲男人的天堂在线aⅴ视频| 久久av老司机精品网站导航| 成人福利视频在线| 一区二区三区av电影| 6080午夜不卡| 粉嫩aⅴ一区二区三区四区| 亚洲天堂2016| 欧美日韩精品欧美日韩精品| 中文字幕不卡的av| 亚洲成人自拍网| 91精品国产高清一区二区三区蜜臀 | 精品福利一区二区三区| 风间由美一区二区三区在线观看 | 91精品国产色综合久久| 成人午夜在线免费| 免费观看日韩av| 亚洲精品精品亚洲| 久久久久9999亚洲精品| 99视频一区二区三区| 视频精品一区二区| 日本一区二区三区视频视频| 婷婷综合另类小说色区| 国产精品午夜久久| 久久久久久久久久久久电影| 欧美日韩视频在线第一区| 国产亲近乱来精品视频| 日韩欧美123| 欧美视频在线一区| 色视频一区二区| 亚洲影院久久精品| 国产精品理伦片| 国产视频911| 欧美激情一区二区三区在线| 日韩欧美中文字幕制服| 奇米综合一区二区三区精品视频| 欧美日韩黄色影视| 99精品视频一区| 777亚洲妇女| 欧美mv日韩mv| 久久婷婷久久一区二区三区| 日韩久久久精品| 久久综合九色综合97婷婷| 精品三级av在线| 久久视频一区二区| 国产亚洲精品资源在线26u| 欧美一区二区日韩一区二区| 69p69国产精品| 亚洲国产精品二十页| 亚洲欧美在线高清| 日精品一区二区| 国产久卡久卡久卡久卡视频精品| 亚洲女性喷水在线观看一区| 91麻豆精品国产自产在线| 欧美韩国日本一区| 午夜精品久久久久久久| 中文字幕成人av| 亚洲一区二区三区自拍| 精品国产乱码久久久久久久 | 久久超碰97中文字幕| 国产99精品视频| 欧美日韩电影在线播放| 精品免费视频.| 樱花草国产18久久久久| 国产成人精品一区二区三区四区 | 在线欧美日韩精品| 国产精品一区在线| 亚洲高清免费观看高清完整版在线观看| 日韩一级大片在线观看| 中文字幕永久在线不卡| 精品国产区一区| 亚洲二区在线观看| 亚洲一区二区精品3399| zzijzzij亚洲日本少妇熟睡| 日韩一区二区在线看| 亚洲国产精品人人做人人爽| 成人精品一区二区三区中文字幕| 国产老肥熟一区二区三区| 欧美日韩久久久一区| 欧美日韩在线三级| 中文字幕免费观看一区| 精品一区二区三区免费毛片爱| 麻豆国产91在线播放| 欧美一区二区久久| 日韩va亚洲va欧美va久久| 91精品一区二区三区久久久久久| 日韩欧美中文字幕公布| 免费观看一级特黄欧美大片| 91麻豆精品国产91久久久资源速度| 欧美日韩电影一区| 日本午夜一区二区| 欧美军同video69gay| 日欧美一区二区| 成人精品在线视频观看| 欧美色偷偷大香| 日韩高清国产一区在线| 久久蜜桃av一区二区天堂 | 国产在线视视频有精品| 久久久久久久综合狠狠综合| 成人h动漫精品| 一区二区不卡在线视频 午夜欧美不卡在 | 综合精品久久久| 在线观看亚洲精品视频| 日本欧美一区二区三区| 欧美国产精品一区二区三区| 色婷婷av一区二区三区之一色屋| 欧美一区二区三区啪啪| 日韩av电影免费观看高清完整版在线观看 | 国产乱码精品一区二区三区av| 日韩区在线观看| 激情综合网激情|