HelloWorld, the first program of AndEngine

Source: Internet
Author: User

After the AndEngine environment is set up, it is far from enough to run only Samples. To really understand how to use this framework, you still need to write the code by yourself.

Start with a simple HelloWorld.

Purpose: use an image as the background and display "Hello World!" in the center of the screen !" Text.

1. Create an Android project, set project properties, and let the project reference the AndEngine project.

2. Let the main Activity of the project inherit the SimpleBaseGameActivity class. And implement several methods in the base class:

[Java]
Public EngineOptions onCreateEngineOptions ();
Public void onCreateResources ();
Public Scene onCreateScene ();
The code for this class is as follows:
[Java]
Package com. example. testandeng1;
 
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. Entity;
Import org. andengine. entity. scene. Scene;
Import org. andengine. entity. sprite. Sprite;
Import org. andengine. entity. text. Text;
Import org. andengine. opengl. font. Font;
Import org. andengine. opengl. font. FontFactory;
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. region. ITextureRegion;
Import org. andengine. ui. activity. SimpleBaseGameActivity;
 
Import android. graphics. Color;
Import android. opengl. GLES20;
 
Public class MainActivity extends SimpleBaseGameActivity {
Public static final int CAMERA_WIDTH = 800;
Public static final int CAMERA_HEIGHT = 480;
 
Private static final int LAYER_COUNT = 2;
 
Private static final int LAYER_BACKGROUND = 0;
Private static final int LAYER_TEXT = LAYER_BACKGROUND + 1;
 
Private Scene mScene;
 
Private Font mFont;
Private BitmapTextureAtlas mBackgroundTexture;
Private ITextureRegion mBackgroundTextureRegion;

Private Text mHelloText;
 
Public EngineOptions onCreateEngineOptions (){
Camera mCamera = new Camera (0, 0, CAMERA_WIDTH, CAMERA_HEIGHT );
Final EngineOptions engineOptions = new EngineOptions (true,
ScreenOrientation. LANDSCAPE_FIXED, new RatioResolutionPolicy (
CAMERA_WIDTH, CAMERA_HEIGHT), mCamera );
Return engineOptions;
}
 
@ Override
Public void onCreateResources (){
/* Load the font we are going to use .*/
FontFactory. setAssetBasePath ("font /");
This. mFont = FontFactory. createFromAsset (this. getFontManager (),
This. getTextureManager (), 512,512, TextureOptions. BILINEAR,
This. getAssets (), "GOTHIC. TTF", 32, true, Color. WHITE );
This. mFont. load ();
 
BitmapTextureAtlasTextureRegionFactory. setAssetBasePath ("img /");
/* Load all the textures this game needs .*/
This. mBackgroundTexture = new BitmapTextureAtlas (
This. getTextureManager (), 800,480 );
This. mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory
. CreateFromAsset (this. mBackgroundTexture, this, "android.png ",
0, 0 );
This. mBackgroundTexture. load ();
}
 
@ Override
Public Scene onCreateScene (){
This. mScene = new Scene ();
For (int I = 0; I <LAYER_COUNT; I ++ ){
This. mScene. attachChild (new Entity ());
}
 
/* No background color needed as we have a fullscreen background sprite .*/
This. mScene. setBackgroundEnabled (false );
This. mScene. getChildByIndex (LAYER_BACKGROUND). attachChild (
New Sprite (0, 0, this. mBackgroundTextureRegion, this
. GetVertexBufferObjectManager ()));

This. mHelloText = new Text (CAMERA_WIDTH/2-80, CAMERA_HEIGHT/2-16, this. mFont, "Hello World! ", This. getVertexBufferObjectManager ());
This. mHelloText. setBlendFunction (gles‑gl _ SRC_ALPHA, gles‑gl _ ONE_MINUS_SRC_ALPHA );
This. mHelloText. setAlpha (0.6f );
This. mScene. getChildByIndex (LAYER_TEXT). attachChild (this. mHelloText );

Return this. mScene;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.