1.回顾

       在上一节已经成功的导入AndEngine源代码项目,我们就利用它来实现我们的工程.lib文件在bin目录下:

 

2.建立工程

     在eclipse下file->new->project...->Android Application Project

 

 点击next

设置工程明等参数,例如:MoveBall,为了兼容工程版本,将SDk版本修改为2.1,如图所示:

 

接着下一步,可以随便选择你要的图标

 

然后next ....finish就完成了初始工程的创建

 

3.修改原始工程

 鼠标放在MoveBall项目上,右键选择Build Path->Configure build path

然后选择Projects,点击右边的Add.选择上AndEngine


点击OK就可以将AndEngine项目添加到工程了


打开MoveBall,java,将MoveBall extends Activity修改为MoveBall extends BaseGameActivity.

接着写代码:

[java]  view plain copy
  1. package season.lxx.moveball;  
  2.   
  3.   
  4. import org.andengine.engine.camera.Camera;  
  5. import org.andengine.engine.options.EngineOptions;  
  6. import org.andengine.engine.options.ScreenOrientation;  
  7. import org.andengine.engine.options.resolutionpolicy.IResolutionPolicy;  
  8. import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;  
  9. import org.andengine.entity.scene.Scene;  
  10. import org.andengine.entity.scene.background.RepeatingSpriteBackground;  
  11. import org.andengine.entity.sprite.AnimatedSprite;  
  12. import org.andengine.entity.sprite.TiledSprite;  
  13. import org.andengine.entity.sprite.vbo.ITiledSpriteVertexBufferObject;  
  14. import org.andengine.opengl.texture.TextureOptions;  
  15. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;  
  16. import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;  
  17. import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;  
  18. import org.andengine.opengl.texture.region.ITiledTextureRegion;  
  19. import org.andengine.opengl.texture.region.TiledTextureRegion;  
  20. import org.andengine.opengl.vbo.VertexBufferObjectManager;  
  21. import org.andengine.ui.activity.BaseGameActivity;  
  22.   
  23. import android.app.Activity;  
  24. import android.util.Log;  
  25. import android.view.Menu;  
  26. import android.view.MenuItem;  
  27. import android.support.v4.app.NavUtils;  
  28.   
  29. public class MoveBall extends BaseGameActivity {  
  30.   
  31.     private static final int CAMERA_WIDTH = 800;  
  32.     private static final int CAMERA_HEIGHT = 480;  
  33.     private final static float BALL_VELOCITY = 100f;//球的移动速度  
  34.       
  35.     private Camera mCamera;  
  36.     private Scene mScene;  
  37.     private RepeatingSpriteBackground background;  
  38.     private TiledTextureRegion mFaceTextureRegion;  
  39.       
  40.     @Override  
  41.     public EngineOptions onCreateEngineOptions() {  
  42.         // TODO Auto-generated method stub  
  43.           
  44.         mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);  
  45.         EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);  
  46.           
  47.         return mEngineOptions;  
  48.     }  
  49.   
  50.     @Override  
  51.     public void onCreateResources(  
  52.             OnCreateResourcesCallback pOnCreateResourcesCallback)  
  53.             throws Exception {  
  54.         // TODO Auto-generated method stub  
  55.         this.background = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT,    
  56.                    getTextureManager(), AssetBitmapTextureAtlasSource.create(    
  57.                     this.getAssets(), "background.png"),    
  58.                     getVertexBufferObjectManager());   
  59.           
  60.           
  61.         BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(),64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
  62.         mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this"face_circle_tiled.png"00,2,1);  
  63.           
  64.         /** 
  65.          * 参数说明: 
  66.          * mTexure是在内存中放置贴图资源用的,64,32是图片要求的宽和高,必须是2的n次方大小.如:2,4,8,16,32,64,128,512,1024.... 
  67.          * 并且要比原图的宽高要大 
  68.          *  
  69.          * mFaceTextureRegion相当于从mTexure中扣图,因为mTexure是由很多图集组成的,要从中截取一片出来 
  70.          * 0,0代表截图的top,right坐标(起点坐标),2和1分别代表贴图中一张存在2列1行 
  71.          *  
  72.          */  
  73.         mTexture.load();  
  74.           
  75.         pOnCreateResourcesCallback.onCreateResourcesFinished();  
  76.     }  
  77.   
  78.     @Override  
  79.     public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)  
  80.             throws Exception {  
  81.         // TODO Auto-generated method stub  
  82.         mScene = new Scene();  
  83.         mScene.setBackground(background);  
  84.           
  85.         final float centerX = (CAMERA_WIDTH - mFaceTextureRegion.getWidth()) / 2;//计算贴图的中心坐标  
  86.         final float centerY = (CAMERA_HEIGHT - mFaceTextureRegion.getHeight()) / 2;  
  87.         final Ball mBall = new Ball(centerX, centerY,3232,this.mFaceTextureRegion,getVertexBufferObjectManager());  
  88.           
  89.         mScene.attachChild(mBall);  
  90.         pOnCreateSceneCallback.onCreateSceneFinished(mScene);  
  91.     }  
  92.   
  93.     @Override  
  94.     public void onPopulateScene(Scene pScene,  
  95.             OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {  
  96.         // TODO Auto-generated method stub  
  97.           
  98.           
  99.           
  100.         pOnPopulateSceneCallback.onPopulateSceneFinished();  
  101.     }  
  102.   
  103.       
  104.     private static class Ball extends AnimatedSprite{  
  105.              
  106.           
  107.         float mVelocityX = BALL_VELOCITY;//球的x方向速度  
  108.         float mVelocityY = BALL_VELOCITY ;//球的y方向速度  
  109.   
  110.         public Ball(float pX, float pY, float pWidth, float pHeight,  
  111.                 ITiledTextureRegion pTiledTextureRegion,  
  112.                 VertexBufferObjectManager pVertexBufferObjectManager) {  
  113.             super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager);  
  114.             // TODO Auto-generated constructor stub  
  115.             mX = 100;  
  116.             mY = 100;  
  117.         }  
  118.   
  119.         @Override  
  120.         protected void onManagedUpdate(float pSecondsElapsed) {  
  121.             // TODO Auto-generated method stub  
  122.               
  123.                 if(this.mX < 0) {  
  124.                     setVelocityX(BALL_VELOCITY);  
  125.                 } else if(  this.mX + this.getWidth() > CAMERA_WIDTH){  
  126.                     setVelocityX(-BALL_VELOCITY);  
  127.   
  128.                 }  
  129.   
  130.                 if(this.mY < 0 ) {  
  131.                     setVelocityY(BALL_VELOCITY);  
  132.                 } else if(this.mY + this.getHeight() > CAMERA_HEIGHT){  
  133.                     setVelocityY(-BALL_VELOCITY);  
  134.                 }  
  135.                   
  136.               
  137.   
  138.                 mX += mVelocityX * pSecondsElapsed;  
  139.                 mY += mVelocityY * pSecondsElapsed;  
  140.                   
  141.                   
  142.                 this.setPosition(mX, mY);  
  143.             Log.d("Season",pSecondsElapsed + "");  
  144.               
  145.             super.onManagedUpdate(pSecondsElapsed);  
  146.               
  147.               
  148.         }  
  149.           
  150.         void setVelocityX(float vx){  
  151.               
  152.             mVelocityX = vx;  
  153.         }  
  154.           
  155.           
  156.         void setVelocityY(float vy){  
  157.             mVelocityY = vy;  
  158.         }  
  159.           
  160.           
  161.           
  162.           
  163.     }  
  164.       
  165.       
  166. }  

很重要的一步,为了让游戏顺利运行,一定要将AndEngine/bin/andengine.jar拷贝到MoveBall/libs下



本例子用到两张图片:



把这两张图片拷贝到MoveBall/assets目录下


然后运行就可以看到一个运动的小脸蛋了.呵呵



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

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐