Android使用AndEngine建立第一個程式

來源:互聯網
上載者:User

首先要把andengine.jar複製到libs檔案夾裡

 
01
package com.hu.anden;
02
 
03
import org.anddev.andengine.engine.Engine;
04
import org.anddev.andengine.engine.camera.Camera;
05
import org.anddev.andengine.engine.options.EngineOptions;
06
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
07
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
08
import org.anddev.andengine.entity.scene.Scene;
09
import org.anddev.andengine.entity.sprite.Sprite;
10
import org.anddev.andengine.opengl.texture.TextureOptions;
11
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
12
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
13
import org.anddev.andengine.opengl.texture.region.TextureRegion;
14
import org.anddev.andengine.ui.activity.BaseGameActivity;
15
 
16
public class MainActivity extends BaseGameActivity {
17
 
18
    public static int CAMERA_WIDTH = 320;
19
    public static int CAMERA_HEIGHT = 480;
20
    
21
    public Camera mCamera;
22
    public Scene mScene;
23
    private BitmapTextureAtlas bgTexture;
24
    private TextureRegion background;
25
    
26
    public Engine onLoadEngine() {
27
        this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);//建立相機
28
        return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT,
29
                new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
30
                this.mCamera));
31
    }
32
 
33
    public void onLoadResources() {
34
        bgTexture = new BitmapTextureAtlas(1024, 1024,
35
                TextureOptions.BILINEAR_PREMULTIPLYALPHA);
36
        background = BitmapTextureAtlasTextureRegionFactory.createFromAsset(//從資產讀取圖片
37
                bgTexture, this, "colorful.png", 0, 0);
38
        this.getEngine().getTextureManager().loadTextures(bgTexture);
39
    }
40
 
41
    public Scene onLoadScene() {
42
        mScene = new Scene();
43
        mScene.setTouchAreaBindingEnabled(true);
44
        return mScene; www.2cto.com
45
    }
46
 
47
    public void onLoadComplete() {
48
        Sprite bgd = new Sprite(0, 0, background);
49
        mScene.attachChild(bgd);
50
    }
51
    
52
}


作者:叢林聽雨

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.