(转)[AndEngine学习教程] 第7节 场景精灵间的坐标转换

1.要点分析

      1.在制作动画精灵的时候,为了方便计算,常常需要把场景中的坐标转换为精灵的内部坐标,

        或者需要把精灵的内部坐标转换为场景坐标.如果精灵没有进行过旋转操作,他们之间只差

         一个offse而已

      2.本节需要实现的是在一个人脸精灵内,找到他的眼睛位置,然后随意变换精灵(大小比例,旋转,移动等),

          然后依旧定位出精灵的眼睛.

2.新内容

      1.要在Activity上标注精灵的眼睛,需要使用到箭头,在简略的情况下,可以用三根直线实现,好在AndEngine

         已经为我们提供一Line类:public class Line extends Shape

        

[java]  view plain copy
  1. /** 
  2.      * Uses a default {@link HighPerformanceLineVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Line#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. 
  3.      */  
  4.     public Line(final float pX1, final float pY1, final float pX2, final float pY2, final VertexBufferObjectManager pVertexBufferObjectManager) {  
  5.         this(pX1, pY1, pX2, pY2, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager, DrawType.STATIC);  
  6.     }  
  7.   
  8.     /** 
  9.      * Uses a default {@link HighPerformanceLineVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Line#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. 
  10.      */  
  11.     public Line(final float pX1, final float pY1, final float pX2, final float pY2, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) {  
  12.         this(pX1, pY1, pX2, pY2, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager, pDrawType);  
  13.     }  
  14.   
  15.     /** 
  16.      * Uses a default {@link HighPerformanceLineVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Line#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}. 
  17.      */  
  18.     public Line(final float pX1, final float pY1, final float pX2, final float pY2, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) {  
  19.         this(pX1, pY1, pX2, pY2, pLineWidth, pVertexBufferObjectManager, DrawType.STATIC);  
  20.     }  
  21.   
  22.     public Line(final float pX1, final float pY1, final float pX2, final float pY2, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) {  
  23.         this(pX1, pY1, pX2, pY2, pLineWidth, new HighPerformanceLineVertexBufferObject(pVertexBufferObjectManager, Line.LINE_SIZE, pDrawType, true, Line.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT));  
  24.     }  
  25.   
  26.     public Line(final float pX1, final float pY1, final float pX2, final float pY2, final float pLineWidth, final ILineVertexBufferObject pLineVertexBufferObject) {  
  27.         super(pX1, pY1, PositionColorShaderProgram.getInstance());  

      2.AnalogOnScreenControl类,这个我们在上一节的内容中已经详细介绍了,使用这个类只是方便我们观看坐标切换后的效果

3.源代码分析

        1.内部成员变量定义:背景+控制器+精灵脸

            

[java]  view plain copy
  1. private static final float CAMERA_WIDTH = 800;  
  2.     private static final float CAMERA_HEIGHT = 480;  
  3.       
  4.     private Camera mCamera;  
  5.     private RepeatingSpriteBackground mBackground;  
  6.     private TiledTextureRegion mBaseRegion;  
  7.     private TiledTextureRegion mKnobRegion;  
  8.     private TiledTextureRegion mFaceRegion;  
  9.       
  10.       
  11.     @Override  
  12.     public EngineOptions onCreateEngineOptions() {  
  13.         // TODO Auto-generated method stub  
  14.         mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);  
  15.         EngineOptions mEngineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),mCamera);  
  16.         return mEngineOptions;  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onCreateResources(  
  21.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  22.             throws Exception {  
  23.         // TODO Auto-generated method stub  
  24.         mBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, getTextureManager(), AssetBitmapTextureAtlasSource.create(getAssets(), "background_grass.png"), getVertexBufferObjectManager());  
  25.           
  26.         BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(), 256128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
  27.           
  28.         mBaseRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this"onscreen_control_base.png"0011);  
  29.         mKnobRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this"onscreen_control_knob.png",128,0,11);  
  30.         mFaceRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this"face_box.png"192011);  
  31.           
  32.         mTexture.load();  
  33.           
  34.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  35.     }  
在这里,控制器的背景,控制器指针和精灵脸蛋共用一个Texture.

     

         2.在onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)函数中,定义了3条直线,一个人脸精灵和一个控制器

           

[java]  view plain copy
  1. Scene mScene = new Scene();  
  2.         mScene.setBackground(mBackground);  
  3.           
  4.         final Line mDownLine = new Line(0,0,0,0,3,getVertexBufferObjectManager());  
  5.               mDownLine.setColor(100);  
  6.         final Line mLeftLine = new Line(0,0,0,0,3,getVertexBufferObjectManager());  
  7.               mLeftLine.setColor(100);  
  8.         final Line mRightLine = new Line(0,0,0,0,3,getVertexBufferObjectManager());  
  9.               mRightLine.setColor(100);  
  10.           
  11.         final MySprite mFace = new MySprite(100100,mFaceRegion, getVertexBufferObjectManager()){  
  12.   
  13.             @Override  
  14.             protected void onManagedUpdate(float pSecondsElapsed) {  
  15.                 // TODO Auto-generated method stub  
  16.                 float mCoordinates[] = this.convertLocalToSceneCoordinates(1113);//坐标转换  
  17.                 float x = mCoordinates[0];  
  18.                 float y = mCoordinates[1];  
  19.                 mDownLine.setPosition(x, y+50, x, y);  
  20.                 mLeftLine.setPosition(x-10, y+10, x, y);  
  21.                 mRightLine.setPosition(x+10, y+10, x, y);  
  22.                 super.onManagedUpdate(pSecondsElapsed);  
  23.             }  
  24.               
  25.               
  26.         };  


  在这里比较重要的是convertLocalToSceneCoordinates函数,先看看它是如何实现的,这个方法是在Entity类中实现的:

    

[java]  view plain copy
  1. /* (non-Javadoc) 
  2.      * @see org.andengine.entity.IEntity#convertLocalToSceneCoordinates(float[]) 
  3.      */  
  4.     @Override  
  5.     public float[] convertLocalToSceneCoordinates(final float[] pCoordinates) {  
  6.         return this.convertLocalToSceneCoordinates(pCoordinates, Entity.VERTICES_LOCAL_TO_SCENE_TMP);  
  7.     }  
  8.   
  9.     /* (non-Javadoc) 
  10.      * @see org.andengine.entity.IEntity#convertLocalToSceneCoordinates(float[], float[]) 
  11.      */  
  12.     @Override  
  13.     public float[] convertLocalToSceneCoordinates(final float[] pCoordinates, final float[] pReuse) {  
  14.         final Transformation localToSceneTransformation = this.getLocalToSceneTransformation();  
  15.   
  16.         pReuse[Constants.VERTEX_INDEX_X] = pCoordinates[Constants.VERTEX_INDEX_X];  
  17.         pReuse[Constants.VERTEX_INDEX_Y] = pCoordinates[Constants.VERTEX_INDEX_Y];  
  18.   
  19.         localToSceneTransformation.transform(pReuse);  
  20.   
  21.         return pReuse;  
  22.     }  
再跟踪 getLocalToSceneTransformation()  :

       

[java]  view plain copy
  1. @Override  
  2.     public Transformation getLocalToSceneTransformation() {  
  3.         if(this.mLocalToSceneTransformation == null) {  
  4.             this.mLocalToSceneTransformation = new Transformation();  
  5.         }  
  6.   
  7.         // TODO Cache if parent(recursive) not dirty.  
  8.         final Transformation localToSceneTransformation = this.mLocalToSceneTransformation;  
  9.         localToSceneTransformation.setTo(this.getLocalToParentTransformation());  
  10.   
  11.         final IEntity parent = this.mParent;  
  12.         if(parent != null) {  
  13.             localToSceneTransformation.postConcat(parent.getLocalToSceneTransformation());  
  14.         }  
  15.   
  16.         return localToSceneTransformation;  
  17.     }  
这就是经过一系列的转换得到的


    3.布置AnalogOnScreenControl:

          

[java]  view plain copy
  1. AnalogOnScreenControl mController = new AnalogOnScreenControl(30, CAMERA_HEIGHT - mBaseRegion.getHeight() - 30, mCamera, mBaseRegion, mKnobRegion, 0.1f, 100, getVertexBufferObjectManager(),   
  2.                 new IAnalogOnScreenControlListener(){  
  3.   
  4.                     @Override  
  5.                     public void onControlChange(  
  6.                             BaseOnScreenControl pBaseOnScreenControl,  
  7.                             float pValueX, float pValueY) {  
  8.                         // TODO Auto-generated method stub  
  9.                         mFace.setVelocityXY(pValueX*100,pValueY*100);  
  10.                     }  
  11.   
  12.                     @Override  
  13.                     public void onControlClick(  
  14.                             AnalogOnScreenControl pAnalogOnScreenControl) {  
  15.                         // TODO Auto-generated method stub  
  16.                           
  17.                     }  
  18.               
  19.               
  20.         });  

4.将各种角色添加到场景中

     

[java]  view plain copy
  1. mFace.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(new ScaleModifier(31.0f, 4.5f),  
  2.                 new RotationModifier(50360),new ScaleModifier(34.5f, 1.0f),new RotationModifier(23600))));  
  3.         mScene.attachChild(mFace);  
  4.         mScene.attachChild(mDownLine);  
  5.         mScene.attachChild(mLeftLine);  
  6.         mScene.attachChild(mRightLine);  
  7.         mScene.setChildScene(mController);  
  8.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
在这里,精灵脸蛋通过修改器来实现缩放和旋转,移动是用过控制器来实现的


5.最后是自定义的精灵类,主要是一点点的修改而已

    

[java]  view plain copy
  1. public class  MySprite extends TiledSprite{  
  2.   
  3.       private float mVelocityX = 0;  
  4.       private float mVelocityY = 0;  
  5.         
  6.     public MySprite(float pX, float pY,  
  7.             ITiledTextureRegion pTiledTextureRegion,  
  8.             VertexBufferObjectManager pVertexBufferObjectManager) {  
  9.         super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);  
  10.         // TODO Auto-generated constructor stub  
  11.           
  12.     }  
  13.   
  14.     @Override  
  15.     protected void onManagedUpdate(float pSecondsElapsed) {  
  16.         // TODO Auto-generated method stub  
  17.         this.mX += mVelocityX * pSecondsElapsed;  
  18.         this.mY += mVelocityY * pSecondsElapsed;  
  19.           
  20.         this.setPosition(mX, mY);  
  21.         super.onManagedUpdate(pSecondsElapsed);  
  22.     }  
  23.          
  24.     void setVelocityXY(float vX, float vY){  
  25.         mVelocityX = vX;  
  26.         mVelocityY = vY;  
  27.     }  
  28.       
  29.    }  

3.测试

    经过层层写代码,是时候看看效果啦大笑,每次到这里都是最开心的时刻哦

   转载请注明出处哦!谢谢,made by Season, 2012-11-2 14:25








本节例子源代码:http://download.csdn.net/detail/cen616899547/4723090



转自:http://blog.csdn.net/cen616899547/article/details/8140491

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值