[AndEngine learning tutorial] section 2nd use AndEngine. jar to create a project

Source: Internet
Author: User

1. Review
After successfully importing the AndEngine source code project in the previous section, we will use it to implement our project. lib file in the bin directory:

 
2. Create a project
In eclipse, file-> new-> project...-> Android Application Project

 
Click next
Set parameters such as project description, for example, MoveBall. to be compatible with the project version, change the SDk version to 2.1 ,:

 
Next, you can select the desired icon.
 
Next... finish, and the initial project is created.
 
3. modify the original project
Right-click the MoveBall project and choose Build Path> Configure build path.
Select Projects and click Add on the right to select AndEngine.

Click OK to add the AndEngine project to the project.

Open MoveBall, java, and change MoveBall extends Activity to MoveBall extends BaseGameActivity.
Then write the code:
[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; // ball moving speed

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 );

/**
* Parameter description:
* MTexure is used to place texture Resources in the memory. 64,32 indicates the width and height required by the image, and must be the n size of 2. such as: 2, 4, 8, 16, 32, 64, 128,512,102 4 ....
* It is larger than the width and height of the source image.
*
* MFaceTextureRegion is equivalent to deducting a graph from mTexure, because mTexure is composed of many atlas and needs to be captured.
* 0, 0 represents the top, right coordinates (starting point coordinates), and 2 and 1 represent two columns and one row in the texture respectively.
*
*/
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; // calculates the center coordinate of the texture
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; // The x-Direction Speed of the ball
Float mVelocityY = BALL_VELOCITY; // y-Direction Speed of the ball
 
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 synchronized condselapsed ){
// 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 * specify condselapsed;
MY + = mVelocityY * extends condselapsed;


This. setPosition (mX, mY );
Log. d ("Season", using condselapsed + "");

Super. onManagedUpdate (descricondselapsed );


}

Void setVelocityX (float vx ){

MVelocityX = vx;
}


Void setVelocityY (float vy ){
MVelocityY = vy;
}




}


}
An important step is to copy AndEngine/bin/andengine. jar to MoveBall/libs to make the game run smoothly.


Two images are used in this example:


Copy the two images to the MoveBall/assets Directory.

Then you can see a face of sports.

 

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.