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.
接着写代码:
- 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() {
-
-
- 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 {
-
- 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);
-
-
-
-
-
-
-
-
-
-
- mTexture.load();
-
- pOnCreateResourcesCallback.onCreateResourcesFinished();
- }
-
- @Override
- public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
- throws Exception {
-
- 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 {
-
-
-
-
- pOnPopulateSceneCallback.onPopulateSceneFinished();
- }
-
-
- private static class Ball extends AnimatedSprite{
-
-
- float mVelocityX = BALL_VELOCITY;
- float mVelocityY = BALL_VELOCITY ;
-
- public Ball(float pX, float pY, float pWidth, float pHeight,
- ITiledTextureRegion pTiledTextureRegion,
- VertexBufferObjectManager pVertexBufferObjectManager) {
- super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager);
-
- mX = 100;
- mY = 100;
- }
-
- @Override
- protected void onManagedUpdate(float pSecondsElapsed) {
-
-
- 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
转自:http://blog.csdn.net/cen616899547/article/details/8127205