Android 游戏引擎AndEngine(加载场景)
package gongzibai.co.cc;
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.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
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 V3Activity extends
BaseGameActivity {
//与图片像素一致
int Camera_WIDTH = 410;
int Camera_HEIGHT = 308;
Camera mCamera;
Texture mTexture;
TextureRegion mTextureRegion;
/** Called when the activity is first created. */
@Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
mCamera = new Camera(0, 0,
Camera_WIDTH,
Camera_HEIGHT);
//设置屏幕水平方向
return new Engine(
new EngineOptions(
true,
ScreenOrientation.LANDSCAPE,
new RatioResolutionPolicy(
Camera_WIDTH,
Camera_HEIGHT),
mCamera));
}
@Override
public void onLoadResources() {
// TODO Auto-generated method stub
//必须设置为 (i.e. 32, 64, 128, 256, 512, 1024).
mTexture = new Texture(
512,
512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
//加载图片资源
mTextureRegion = TextureRegionFactory
.createFromAsset(
mTexture, this,
"gfx/Icon.jpg",
0, 0);
mEngine.getTextureManager()
.loadTexture(mTexture);
}
@Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
//与中心为场景贴图
mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene(1);
final int centerX = (Camera_WIDTH - mTextureRegion
.getWidth())/2 ;
final int centerY = (Camera_HEIGHT - mTextureRegion
.getHeight())/2 ;
final Sprite sprite = new Sprite(
centerX, centerY,
mTextureRegion);
scene.getLastChild()
.attachChild(sprite);
return scene;
}
@Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
}