Step 1: extends BaseGameActivity on AndEngine
Step 6: Buil applition:Step 2: Declaration variablePHP:onLoadEngine
onLoadResources
onLoadScene
onLoadCompleteStep 3: new variable on methodPHP:private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mFaceTextureRegion;Step 4: Load Resources on Method onLoadResourcesPHP:public Engine onLoadEngine() {
// TODO Auto-generated method stub
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
this.mCamera));
}Step 5: Load game on onLoadScenePHP:@Override
public void onLoadResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 32,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(this.mBitmapTextureAtlas, this,
"face_circle_tiled.png", 0, 0, 2, 1);
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}PHP:@Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion
.getHeight()) / 2;
final Ball ball = new Ball(centerX, centerY, this.mFaceTextureRegion);
Log.d("FishingonLoadScene", "onLoadScene dx = " + ball.getX()
+ "dy = " + ball.getY());
scene.attachChild(ball);
return scene;
}Insert class Ball:Notes library on using project:PHP:private static class Ball extends AnimatedSprite {
private final PhysicsHandler mPhysicsHandler;
public Ball(final float pX, final float pY,
final TiledTextureRegion pTextureRegion) {
super(pX, pY, pTextureRegion);
this.mPhysicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(this.mPhysicsHandler);
mPhysicsHandler.setVelocity(DEMO_VELOCITY, DEMO_VELOCITY);
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
if (this.mX < 0) {
this.mPhysicsHandler.setVelocityX(DEMO_VELOCITY);
} else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
this.mPhysicsHandler.setVelocityX(-DEMO_VELOCITY);
}
if (this.mY < 0) {
this.mPhysicsHandler.setVelocityY(DEMO_VELOCITY);
} else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
this.mPhysicsHandler.setVelocityY(-DEMO_VELOCITY);
}
Log.d("FishingonManagedUpdate", "onManagedUpdate dx = " + this.getX() + "dy = "
+ this.getY());
super.onManagedUpdate(pSecondsElapsed);
}
}PHP:package studycoding.net.movingball;
import org.anddev.andengine.engine.Engine;import org.anddev.andengine.engine.camera.Camera;import org.anddev.andengine.engine.handler.physics.PhysicsHandler;import org.anddev.andengine.engine.options.EngineOptions;import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;import org.anddev.andengine.entity.scene.Scene;import org.anddev.andengine.entity.scene.background.ColorBackground;import org.anddev.andengine.entity.sprite.AnimatedSprite;import org.anddev.andengine.entity.util.FPSLogger;import org.anddev.andengine.opengl.texture.TextureOptions;import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;import org.anddev.andengine.ui.activity.BaseGameActivity;
import android.util.Log;
Xây Dựng Ứng Dụng Game trên Android Sử dụng AndEngine |