一: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. public Engine onLoadEngine() {
  5. return null;
  6. }
  7. public void onLoadResources() {
  8. }
  9. public Scene onLoadScene() {
  10. return null;
  11. }
  12. }

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

先上效果图:

一:AndEngine的小例子 一:AndEngine的小例子

具体代码如下:

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


下一篇:loadrunner运行时设置中清空缓存方法