[AndEngine学习教程] 第2节 使用AndEngine.jar创建工程

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

 

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.

接着写代码:

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


import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.IResolutionPolicy;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.sprite.vbo.ITiledSpriteVertexBufferObject;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;

import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MoveBall extends BaseGameActivity {

	private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;
	private final static float BALL_VELOCITY = 100f;//球的移动速度
	
	private Camera mCamera;
	private Scene mScene;
	private RepeatingSpriteBackground background;
	private TiledTextureRegion mFaceTextureRegion;
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		
		mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
		EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
		
		return mEngineOptions;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		// TODO Auto-generated method stub
		this.background = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT,  
	               getTextureManager(), AssetBitmapTextureAtlasSource.create(  
				    this.getAssets(), "background.png"),  
				    getVertexBufferObjectManager()); 
		
		
		BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(),64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "face_circle_tiled.png", 0, 0,2,1);
		
		/**
		 * 参数说明:
		 * mTexure是在内存中放置贴图资源用的,64,32是图片要求的宽和高,必须是2的n次方大小.如:2,4,8,16,32,64,128,512,1024....
		 * 并且要比原图的宽高要大
		 * 
		 * mFaceTextureRegion相当于从mTexure中扣图,因为mTexure是由很多图集组成的,要从中截取一片出来
		 * 0,0代表截图的top,right坐标(起点坐标),2和1分别代表贴图中一张存在2列1行
		 * 
		 */
		mTexture.load();
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		// TODO Auto-generated method stub
		mScene = new Scene();
		mScene.setBackground(background);
		
		final float centerX = (CAMERA_WIDTH - mFaceTextureRegion.getWidth()) / 2;//计算贴图的中心坐标
		final float centerY = (CAMERA_HEIGHT - mFaceTextureRegion.getHeight()) / 2;
		final Ball mBall = new Ball(centerX, centerY,32, 32,this.mFaceTextureRegion,getVertexBufferObjectManager());
		
		mScene.attachChild(mBall);
		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		
		
		
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	
	private static class Ball extends AnimatedSprite{
           
		
		float mVelocityX = BALL_VELOCITY;//球的x方向速度
		float mVelocityY = BALL_VELOCITY ;//球的y方向速度

		public Ball(float pX, float pY, float pWidth, float pHeight,
				ITiledTextureRegion pTiledTextureRegion,
				VertexBufferObjectManager pVertexBufferObjectManager) {
			super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager);
			// TODO Auto-generated constructor stub
			mX = 100;
			mY = 100;
		}

		@Override
		protected void onManagedUpdate(float pSecondsElapsed) {
			// TODO Auto-generated method stub
			
				if(this.mX < 0) {
					setVelocityX(BALL_VELOCITY);
				} else if(  this.mX + this.getWidth() > CAMERA_WIDTH){
					setVelocityX(-BALL_VELOCITY);

				}

				if(this.mY < 0 ) {
					setVelocityY(BALL_VELOCITY);
				} else if(this.mY + this.getHeight() > CAMERA_HEIGHT){
					setVelocityY(-BALL_VELOCITY);
				}
				
			

				mX += mVelocityX * pSecondsElapsed;
				mY += mVelocityY * pSecondsElapsed;
				
				
				this.setPosition(mX, mY);
			Log.d("Season",pSecondsElapsed + "");
			
			super.onManagedUpdate(pSecondsElapsed);
			
			
		}
		
		void setVelocityX(float vx){
			
			mVelocityX = vx;
		}
		
		
		void setVelocityY(float vy){
		    mVelocityY = vy;
		}
		
		
		
		
	}
    
    
}

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



 

本例子用到两张图片:



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


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



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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值