andengine的convertLocalCoordinatesToSceneCoordinates方法

使用Tile地图,看过andengine中的例子,都会发现例子中有这么一段话,以前版本的是convertLocalToSceneCoordinates方法。

  1. scene.registerUpdateHandler(new IUpdateHandler() {  
  2.             @Override  
  3.             public void reset() { }  
  4.   
  5.             @Override  
  6.             public void onUpdate(final float pSecondsElapsed) {  
  7.                 /* Get the scene-coordinates of the players feet. */  
  8.                 final float[] playerFootCordinates = player.convertLocalCoordinatesToSceneCoordinates(161);//danielinbiti注1  
  9.                 /* Get the tile the feet of the player are currently waking on. */  
  10.                 final TMXTile tmxTile = tmxLayer.getTMXTileAt(playerFootCordinates[Constants.VERTEX_INDEX_X], playerFootCordinates[Constants.VERTEX_INDEX_Y]);  
  11.                 if(tmxTile != null) {  
  12.                     // tmxTile.setTextureRegion(null); <-- Eraser-style removing of tiles =D  
  13.                     currentTileRectangle.setPosition(tmxLayer.getTileX(tmxTile.getTileColumn()), tmxLayer.getTileY(tmxTile.getTileRow()));  
  14.                 }  
  15.             }  
  16.         });  


对于“注1”中的话,始终没有理解这里为什么写16,1,看上面的英文注释,表达很清楚,就是转换成屏幕坐标。

可能是原来没有用过引擎,理解能力偏低。始终没有理解这里填写16,1是怎么来的,这两个数字是随便写的还是怎么计算出来的。看里面的算法,也没什么疑问。

于是网上找资料,上andengine论坛,看着一篇篇的英文帖子,越看越模糊,数字五花八门,没有任何规律。

哎,没有规律就对了,一开始就纠结到了传参上,这已经落入了陷阱,范了错误。实际这个函数的意思很好理解,就是把local坐标转换成scene坐标。这个local相对与谁呢,实际上就是

  1. player.convertLocalCoordinatesToSceneCoordinates  

这句话的前半部分,player,也就是括号中填写的坐标是相对于player来说的,坐标系在屏幕的左下角,往后是X轴,往上是Y轴。括号中的参数就是相对于player的偏移量。

经过验证后,就是这么一回事。

一开始没有发现,也是因为这是tilemap,根据格子画的矩形,因此,如果这个数字的偏移量不大于一个格子的像素的花,矩形一直都在原地,看不出任何变化。也就导致小范围改动数字都看不出效果来,改动太大了一下子又跑没了。

特此记录,以便于自己涨记性,对于一样的后来者能够立刻醒悟过来。

总算可以踏实的进行下一步了。

原文地址:https://www.cnblogs.com/android100/p/andengine-convertLocalCoordinatesToSceneCoordinates.html