[AndEngine學習教程] 第2節 使用AndEngine.jar創建工程 – JAVA編程語言程序開發技術文章

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] 
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目錄下

然後運行就可以看到一個運動的小臉蛋瞭.呵呵

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。