Bài đăng phổ biến

Thứ Sáu, 26 tháng 10, 2012

AndEngine Bài 1 - Create a Scene

AndEngine là một open - source free cho android 2D Opengl Engine. Nó được đưa ra vào giữa năm 2010 bởi Nicolas Gramlich. Đến bây giờ đã được hơn 2 năm và nó có rất nhiều sự thay đổi, và trở thành 1 trong những Engine được sử dụng rộng rãi cho Game 2D android. Trong bài này tôi sẽ hướng dẫn các bạn tạo 1 Scene


package com.PerleDevelopment.AndEngine.tutorial;
 
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.ui.activity.SimpleBaseGameActivity;
 
public class AndEngineTutorialActivity extends SimpleBaseGameActivity {
    // ===========================================================
    // Constants
    // ===========================================================
    static final int CAMERA_WIDTH = 480;
    static final int CAMERA_HEIGHT = 800;
 
    // ===========================================================
    // Fields
    // ===========================================================
 
    private Camera mCamera;
    private Scene mMainScene;
 
    // ===========================================================
    // Constructors
    // ===========================================================
 
    // ===========================================================
    // Getter & Setter
    // ===========================================================
 
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
 
    @Override
    public EngineOptions onCreateEngineOptions() {
        this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
 
        return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
    }
 
    @Override
    protected void onCreateResources() {
        // TODO Auto-generated method stub
 
    }
 
    @Override
    protected Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger()); // logs the frame rate
        this.mMainScene = new Scene();
 
        return this.mMainScene;
    }
 
}


Đầu tiên chúng ta tạo 1 Project Android đặt tên là AndEngineTutorial1 chẳng hạn và sau đó chúng ta đặt tên Activity là AndEngineTutorial1Activity chẳng hạn. Sau khi tạo Project thành công thì ta có lớp Activity. Chúng ta xóa hết thông tin bên trong lớp và kế thừa lớp SimpleBaseGameActivity như đoạn code trên. Sau đó chương trình yêu cầ bạn cài đặt method Abstract của super class. Bạn click vào lớp và Add unimplemented method --> Kết quả tạo ra 3 method như sau:




Tiếp theo chúng ta tạo ra 2 hằng CAMERA_WIDTH và CAMERA_HEIGHT. Nếu bạn biết chiều rộng và chiều cao của màn hình smartphone của bạn thì bạn khai báo. Trong bài sau tôi sẽ hướng dẫn các bạn lấy chiều cao và rộng của Smartphone.

Tiếp sau là chúng ta định nghĩa 2 biến đối tượng là Camera và Scene

Bây giờ chúng ta hãy nghiên cứu 3 method trong bài này:


  • onCreateEngineOption()
               + Được gọi khi game Engine được loaded
               +  Khởi tạo camera
                          - Camera(pX,pY,pWidth,pHeight)
                          - pX, Py là tọa độ của gốc tọa độ camera
                          - pWidth, pHeight là kich thước trong pixels của camera
               + Khởi tạo Energie
                          - EngineOption(pFullScreen, pScreenOrientation, pResolutionPolicy(pWidth, pHeight),                              pCamera)
                          - pFullScreen: Tùy chọn full screen hay không
                          - pScreenOrientation ở đây chúng ta có thể chọn LANPCAPE hoặc PORTAIR
                          - PRe....Là tỷ lệ của Engine của bạn
                          - Tham số cuối là đối tượng Camera
  • onCreateResource(): Gọi để load tài nguyên
  • onCreateScene():Gọi khi mọi thứ được cài đặt và texture được loaded




























1 nhận xét:

  1. a ơi,e mới tìm hiểu android engine nên chưa biết nhiều... A có thể cho cho e xin mail of a được không? cám ơn a

    Trả lờiXóa