一:AndEngine的小例子

首先导入架包,下载:http://download.csdn.net/detail/duancanmeng/4060082 lib文件夹中

像我们写android程序entends Activity一样,我们一开始也要extends BaseGameActivity,而且实现四个必须被重载函数:

  1. public class Main extends BaseGameActivity {    
  2.     public void onLoadComplete() {    
  3.            
  4.     }    
  5.     public Engine onLoadEngine() {    
  6.         return null;    
  7.     }    
  8.     public void onLoadResources() {    
  9.            
  10.     }    
  11.     public Scene onLoadScene() {    
  12.         return null;    
  13.     }      
  14. }   

 

只需实现上面四个方法,把业务逻辑和数据加进去,便可完成一个简单的小例子。

先上效果图:

 

 

具体代码如下:

  1. public class ShapeModifierExample extends BaseGameActivity{  
  2.   
  3.     private static final int CAMERA_WIDTH = 720;  
  4.     private static final int CAMERA_HEIGHT = 480;  
  5.       
  6.     private Camera mCamera;  
  7.     private Texture mTexture;  
  8.     private TiledTextureRegion mFaceTextureRegion;  
  9.     @Override  
  10.     public Engine onLoadEngine() {  
  11.         Log.i("test","onLoadEngine");  
  12.         //游戏摄像机,在AndEngine的Camera有两种作用,一是用以调节屏幕的显示区域,二是利用HUD类实际绘制游戏屏幕于手机之上。  
  13.         //new Camera中的四个参数,第一个和第二个是代表camera的左顶点,第三个和第四个分别代表camera的宽和高  
  14.         this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);  
  15.           
  16.         //Engine是AndEngine的核心所在,  
  17.         //它对AndEngine引擎中Camera、Scene等重要组件进行了统一管理,  
  18.         //但必须和BaseGameActivity合作使用,  
  19.         //利用EngineOptions类可以对其进行必要的参数配置。  
  20.   
  21.         //其中EngineOptions中的参数:  
  22.         //    第一个判断是否为全屏  
  23.         //    第二个判断屏幕横屏还是竖屏  
  24.         //    第三个RatioResolutionPolicy(按比例修正画面大小,以适应屏幕大小),  
  25.         //    第四个便是我们自己定义的  
  26.         //第三个参数还可以:BaseResolutionPolicy(除了会校验一下屏幕大小外,什么也不做)、  
  27.         //      FillResolutionPolicy(拉伸游戏画面为全屏填充,视摄像机大小不同,会有不同程度变形)、  
  28.         //      FixedResolutionPolicy(强行规定游戏画面为固定大小,此设置不会自动适应屏幕大小),  
  29.         //      RatioResolutionPolicy(按比例修正画面大小,以适应屏幕大小),  
  30.         //      RelativeResolutionPolicy(根据构建RelativeResolutionPolicy时的缩放参数,缩放游戏屏幕为指定比例)。  
  31.         return new Engine(new EngineOptions(true,ScreenOrientation.LANDSCAPE,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),this.mCamera));  
  32.     }  
  33.   
  34.     @Override  
  35.     public void onLoadResources() {  
  36.         Log.i("test","onLoadResources");  
  37.         //该处Texture的构造方法中三个参数:  
  38.         //  第一个:要使用的纹理图片的宽度  
  39.         //  第二个:要使用的纹理图片的高度  
  40.         //  第三个:纹理的渲染方式  
  41.         //第三个参数其中有如下情况:  
  42.         //1、NEAREST(Nearest滤波,实现上依赖GL_NEAREST做不光滑过滤,纹理环绕模式为GL_CLAMP_TO_EDGE,显示速度快画质差)  
  43.         //2、BILINEAR(双线性插值,实现上依赖GL_LINEAR做线性滤波,纹理环绕模式为GL_CLAMP_TO_EDGE,显示速度慢画质佳)  
  44.         //3、REPEATING(与NEAREST同为Nearest滤波,但纹理环绕模式为GL_REPEAT,会自动填充纹理上的空白区域,显示速度较快画质差)  
  45.         //4、REPEATING_BILINEAR(与BILINEAR同为双线性插值,但纹理环绕模式为GL_REPEAT,会自动填充纹理上的空白区域,显示速度很慢画质佳(低端机跑此模式异常悲剧,高端机尚可))  
  46.         //5、NEAREST_PREMULTIPLYALPHA(所有[PREMULTIPLYALPHA]结尾的TextureOptions与其它同名类差别仅在于是否支持根据Alpha值设置透明纹理,以下同)  
  47.         //6、BILINEAR_PREMULTIPLYALPHA  
  48.         //7、REPEATING_PREMULTIPLYALPHA  
  49.         //8、REPEATING_BILINEAR_PREMULTIPLYALPHA等静态对象。  
  50.         //等等  
  51.         this.mTexture = new Texture(64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA);    //前2个参数意义是划出<span style="FONT-SIZE: 12px; LINE-HEIGHT: 17px">                                   //pWidth*</span><span style="FONT-SIZE: 12px; LINE-HEIGHT: 17px">pHeight大小的一块空间用来存储</span><span style="FONT-SIZE: 12px; LINE-HEIGHT: 17px">createFromAsset装载的贴图</span>                              //备注1  
  52.           
  53.         //将纹理图片“贴”到我们上面定义的Texture上  
  54.         //其中的参数:第二个代表本context,第四个:代表纹理贴到Texture上的X坐标,第五个:代表贴到Texture上的Y坐标,第六个:代表纹理要贴的列数,第七个:代表纹理要贴的行数  
  55.         this.mFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this"face_box_tiled.png"0021);  
  56.         this.mEngine.getTextureManager().loadTexture(mTexture);  
  57.     }  
  58.   
  59.     @Override  
  60.     public Scene onLoadScene() {  
  61.         Log.i("test","onLoadScene");  
  62.         //registerUpdateHandler函数注册得到IUpdateHandler接口,内部有onUpdate以及reset两个函数等待实现,几乎所有AndEngine应用中都必然会看到它的身影,它也是AndEngine添加具体业务到游戏业务线程中的主要方法之一。  
  63.         //主要用来供外部方法调用,以便更新业务  
  64.         //可以看到所有的exmaples中都有如此写法  
  65.         this.mEngine.registerUpdateHandler(new FPSLogger());    //备注2  
  66.   
  67.         //场景容器,作用类似于LGame中的Screen,能够将某一特定场景作为游戏模块进行调用,我们可以利用它来切换当前游戏的画面与触摸屏监听,切换方法是利用Engine.setScene  
  68.         final Scene scene = new Scene(1);  
  69.         //这里的颜色的值设置必须在0.0到1.0,例如(1,0,0)为红色  
  70.         scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));  
  71.   
  72.         final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;  
  73.         final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;  
  74.         Log.i("test","centerX:"+centerX+",centerY:"+centerY);  
  75.         //定义一个方块  
  76.         final Rectangle rect = new Rectangle(centerX + 100, centerY, 3232);  
  77.         rect.setColor(100);  
  78.   
  79.         //定义一个动画精灵,并设置一些属性  
  80.         final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion);  
  81.         face.animate(100);  
  82.         face.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);          //备注3  
  83.   
  84.         //主要设置上面定义的两个组件的动作  
  85.         //LoopShapeModifier(final IShapeModifierListener pShapeModiferListener, final int pLoopCount, final ILoopShapeModifierListener pLoopModifierListener, final IShapeModifier pShapeModifier)  
  86.         //从打印的语句可以看出,第一个参数是当整个动作结束的时候所做的一些操作,第二个参数就是动作的循环的次数,第三个就是一个动作完成后所做的一些操作,第四个就是具体的动作  
  87.         final LoopShapeModifier shapeModifier =  
  88.             new LoopShapeModifier(  
  89.                     new IShapeModifierListener() {  
  90.                         @Override  
  91.                         public void onModifierFinished(final IModifier<IShape> pShapeModifier, final IShape pShape) {  
  92.                             ShapeModifierExample.this.runOnUiThread(new Runnable() {  
  93.                                 @Override  
  94.                                 public void run() {  
  95.                                     Toast.makeText(ShapeModifierExample.this"Sequence ended.", Toast.LENGTH_LONG).show();  
  96.                                 }  
  97.                             });  
  98.                         }  
  99.                     },  
  100.                     1,  
  101.                     new ILoopShapeModifierListener() {  
  102.                         @Override  
  103.                         public void onLoopFinished(final LoopModifier<IShape> pLoopShapeModifier, final int pLoopsRemaining) {  
  104.                             ShapeModifierExample.this.runOnUiThread(new Runnable() {  
  105.                                 @Override  
  106.                                 public void run() {  
  107.                                     Toast.makeText(ShapeModifierExample.this"Loops remaining: " + pLoopsRemaining, Toast.LENGTH_SHORT).show();  
  108.                                 }  
  109.                             });  
  110.                         }  
  111.                     },  
  112.                     new SequenceShapeModifier(  
  113.                             //这里面的参数,第一个是Duration,第二个是From,第三个是to  
  114.                             new RotationModifier(1090),  
  115.                             new AlphaModifier(210),  
  116.                             new AlphaModifier(101),  
  117.                             new ScaleModifier(210.5f),  
  118.                             new DelayModifier(0.5f),  
  119.                             //这里是一些组合动作  
  120.                             new ParallelShapeModifier(  
  121.                                     new ScaleModifier(30.5f, 5),  
  122.                                     new RotationByModifier(390)  
  123.                             ),  
  124.                             new ParallelShapeModifier(  
  125.                                     new ScaleModifier(351),  
  126.                                     new RotationModifier(31800)  
  127.                             )  
  128.                     )  
  129.             );  
  130.   
  131.         face.addShapeModifier(shapeModifier);  
  132.         rect.addShapeModifier(shapeModifier.clone());  
  133.   
  134.         scene.getTopLayer().addEntity(face);  
  135.         scene.getTopLayer().addEntity(rect);  
  136.   
  137.         return scene;  
  138.     }  
  139.   
  140.     @Override  
  141.     public void onLoadComplete() {  
  142.         // TODO Auto-generated method stub  
  143.         Log.i("test","onLoadComplete");  
  144.     }  
  145.   
  146. }  
posted @ 2013-06-20 15:39  朱颂东  阅读(827)  评论(1编辑  收藏  举报