分享

AndEngine镜头追随

 幻魂1990 2013-09-11

AndEngine可以为Camera设置Chase entity来使得镜头随entity移动, 而entity始终在屏幕中间位置。

但有个问题, 如果Player出现在(0,CAMERA_HEIGHT-PLAYER_HEIGHT)的位置,屏幕中3/4的位置可能出现黑屏。


这就需要创建自己的Camera,并更改原有的追随方式(以冒险岛游戏中镜头追随方式为例)

MapleStoryCamera.java

  1. public class MapleStoryCamera extends Camera {  
  2.   
  3.     private Sprite mChaseEntity;  
  4.     private float mBoundsXMax = 16*80;  
  5.     private float mBoundsYMax = 16*50;  
  6.       
  7.     public MapleStoryCamera(float pX, float pY, float pWidth, float pHeight) {  
  8.         super(pX, pY, pWidth, pHeight);  
  9.     }  
  10.       
  11.     public MapleStoryCamera(float pX, float pY, float pWidth, float pHeight, float boundsXMax, float boundsYMax){  
  12.         super(pX, pY, pWidth, pHeight);  
  13.         this.mBoundsXMax = boundsXMax;  
  14.         this.mBoundsYMax = boundsYMax;  
  15.     }  
  16.       
  17.     public void updateChaseEntity() {  
  18.         if(mChaseEntity != null) {  
  19.             final float[] centerCoordinates = mChaseEntity.getSceneCenterCoordinates();  
  20.             float x = centerCoordinates[Constants.VERTEX_INDEX_X];  
  21.             float y = centerCoordinates[Constants.VERTEX_INDEX_Y];  
  22.             if(x < this.getWidth()/2){  
  23.                 x = this.getWidth()/2;  
  24.             }  
  25.             if(x > mBoundsXMax - this.getWidth()/2){  
  26.                 x = mBoundsXMax - this.getWidth()/2;  
  27.             }  
  28.             if(y < this.getHeight()/2){  
  29.                 y = this.getHeight()/2;  
  30.             }  
  31.             if(y > mBoundsYMax - this.getHeight()/2){  
  32.                 y = mBoundsYMax - this.getHeight()/2;  
  33.             }  
  34.             super.setCenter(x, y);  
  35.         }  
  36.     }  
  37.       
  38.     public void setChaseEntity(final Sprite pChaseEntity) {  
  39.         super.setChaseEntity(pChaseEntity);  
  40.         this.mChaseEntity = pChaseEntity;  
  41.     }  
  42. }  
修改后得到理想效果




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多