我读现有的一些成熟的游戏的源代码,我来到这个防空火炮:这个平台游戏的滚动代码究竟做了什么?
/* 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