我讀現有的一些成熟的遊戲的源代碼,我來到這個防空火炮:這個平臺遊戲的滾動代碼究竟做了什麼?
/* assign the horizontal position of the TileMap on the screen to offsetX
* this, center aligns the player. */
int offsetX = width/2 - Math.round(player.getX()) - TILE_SIZE;
/* stop the map scrolling if the player reaches the two ends
* of the map */
offsetX = Math.min(offsetX, 0); // if offsetX < 0 , offsetX = 0
offsetX = Math.min(offsetX, width-tileMap.getWidth()); //if offsetX > map width, offsetX = mapwidth
int offsetY = height - toPixels(tileMap.getHeight()); // not really necessary, I think
int firstTile = toTiles(-offsetX); // ???
int lastTile = firstTile + toTiles(width) + 1; // why the +1
我評論過一些地方我想我明白了,問別人的意見的問題。
打擾我大部分的事情是:
1- OFFSETX如何分配(width/2 ... ?
)我已經認識到其指定OFFSETX一些地方,其中心對齊玩家在地圖上,但我不知道如何。
2 - 在第三行中,爲什麼開發人員編寫width - tileMap.getWidth()
?
注意:如果一行一行地解釋代碼太麻煩,請給我一個粗略的想法,也許有圖表?開發人員想要在這裏做什麼。謝謝。
我不明白你的第一個問題 - 你說你懂'寬/ 2'(居中對齊的球員,你是對的,從我可以告訴),和這裏沒有代碼說'player.x = offsetX',這就是我想'怎麼樣...' – Prescott 2011-12-21 04:56:02
'player.x = offsetX'是在這段代碼之後完成的,對不起,我沒有發佈它。我想問的是,公式'width/2 - Math.round(player.getX()) - TILE_SIZE;'如何在屏幕上返回水平位置,該屏幕與播放器居中對齊。我不明白_formula_背後的理論,但我明白它的作用。 – ApprenticeHacker 2011-12-21 05:02:42