Skip to content

Crazy blog

No money, no honey, no funny…


1. Create Android project as normal, class activity example AndEngineActivity

2. Add AndEngine.Jar for build path (right click on Project > properties > java build path)

3. Add user permission in Manifest.xml, after <uses-sdk android:minSdkVersion=”7″ />

<uses-permission android:name=”android.permission.WAKE_LOCK”/>

4. Create folder assets/gfx, then add file: face_box.png (must be png format)

5. Edit code for class AndEngineActivity

package com.mylib.game;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
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.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class AndEngineActivity extends BaseGameActivity {

// ===========================================================
// Constants
// ===========================================================

private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;

// ===========================================================
// Fields
// ===========================================================

private Camera mCamera;
private Texture mTexture;
private TextureRegion mFaceTextureRegion;
private Sprite mFaceToRemove;

@Override
public Engine onLoadEngine() {
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));
}

@Override
public void onLoadResources() {
this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(mTexture, this, “gfx/face_box.png“,0,0);
this.mEngine.getTextureManager().loadTexture(mTexture);
}

@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());

final Scene scene = new Scene(1);
scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

//mFaceToRemove = new Sprite(100,100,mFaceTextureRegion);
final int centerY = (CAMERA_HEIGHT – this.mFaceTextureRegion.getHeight()) / 2;
final int centerX = (CAMERA_WIDTH – this.mFaceTextureRegion.getWidth()) / 2;

Sprite face = new Sprite(centerX, centerY,mFaceTextureRegion) {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() – this.getWidth() / 2, pSceneTouchEvent.getY() – this.getHeight() / 2);
return true;
}
};
//scene.attachChild(mFaceToRemove);
scene.attachChild(face);

scene.registerTouchArea(face);
scene.setTouchAreaBindingEnabled(true);

return scene;
}

@Override
public void onLoadComplete() {

}

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

}

Advertisements

%d bloggers like this: