您的位置:首页 > 其它

andengine背景设置另一种方法

2016-07-27 17:51 288 查看
public class MainActivity extends BaseGameActivity {

private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private TextureRegion mb;
private Camera mCamera;
@Override
public EngineOptions onCreateEngineOptions() {
// TODO 自动生成的方法存根
mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
EngineOptions mEngineOptions = new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
mCamera);
return mEngineOptions;
}
@Override
public void onCreateResources(OnCreateResourcesCallback
pOnCreateResourcesCallback)
throws Exception {
// TODO 自动生成的方法存根
BitmapTextureAtlas mauto = new
BitmapTextureAtlas(getTextureManager(),

1366,768,TextureOptions.BILINEAR_PREMULTIPLYALPHA);//1366、768为图片像素,不能错.

mb=BitmapTextureAtlasTextureRegionFactory.createFromAsset(
mauto, getApplicationContext(), "background.jpg", 0, 0);
mauto.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
@Override
public void onCreateScene(OnCreateSceneCallback
pOnCreateSceneCallback) throws Exception {
// TODO 自动生成的方法存根
Scene scene=new Scene();
AutoParallaxBackground bg=new AutoParallaxBackground(0, 0, 0,
5);//最后一个参数设置每秒钟背景变化速度。
bg.attachParallaxEntity(new
ParallaxEntity(0, 
new Sprite(0, CAMERA_HEIGHT-mb.getHeight(), mb,
getVertexBufferObjectManager())));//第一个参数为移动速度,设为0就不移动了。

scene.setBackground(bg);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback)
throws Exception {
// TODO 自动生成的方法存根
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}

这种方法运用了AutoParallaxBackground方法,意思是背景移动,但如果把移动速度设为0,也可以达到目的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: