AndEngine 后台加载资源的一些问题

一开始没有注意资源加载的问题,但后图形渐渐的增加了,启动画面黑屏的时间越来越长了,终于直接在加载的资源的时候就挂掉了。

加载资源的解决方案,大体搜索了下有这么几种:


第一种:来源于这个网站https://sites.google.com/site/matimdevelopment/splash-screen---easy-way

以下完整代码的整理:


public class SplashTemplate extends BaseGameActivity
{
        private final int CAMERA_WIDTH = 720;
        private final int CAMERA_HEIGHT = 480;

        private Camera camera;
        private Scene splashScene;
        private Scene mainScene;

        private BitmapTextureAtlas splashTextureAtlas;
        private ITextureRegion splashTextureRegion;
        private Sprite splash;

        @Override
        public EngineOptions onCreateEngineOptions()
        {
                camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
                EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), camera);
                return engineOptions;
        }

        @Override
        public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception
        {
                BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
                splashTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.DEFAULT);
                splashTextureRegion =BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, this,"splash.png", 0, 0);
                splashTextureAtlas.load();
                pOnCreateResourcesCallback.onCreateResourcesFinished();
        }

        @Override
        public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throwsException
        {
                initSplashScene();
                pOnCreateSceneCallback.onCreateSceneFinished(this.splashScene);
        }

        @Override
        public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception
        {
                mEngine.registerUpdateHandler(new TimerHandler(3f, new ITimerCallback() 
                {
                    public void onTimePassed(final TimerHandler pTimerHandler) 
                    {
                        mEngine.unregisterUpdateHandler(pTimerHandler);
                        loadResources();
                        loadScenes();         
                        splash.detachSelf();
                        mEngine.setScene(mainScene);
                    }
                }));
                  
                pOnPopulateSceneCallback.onPopulateSceneFinished();
        }
        /**初始化启动画面的场景*/
        private void initSplashScene()
        {
                splashScene = new Scene();
                splash = new Sprite(0, 0, splashTextureRegion, mEngine.getVertexBufferObjectManager())
                {
                        @Override
                        protected void preDraw(GLState pGLState, Camera pCamera)
                        {
                            super.preDraw(pGLState, pCamera);
                            pGLState.enableDither();
                        }
                };
               
                splash.setScale(1.5f);
                splash.setPosition((CAMERA_WIDTH - splash.getWidth()) * 0.5f, (CAMERA_HEIGHT -splash.getHeight()) * 0.5f);

                splashScene.attachChild(splash);
        }
}


第二种: AndEngine 本身也提供了个名为SimpleAsyncGameActivity的类,在org.andengine.ui.activity包下,基本就是直接继承这个类了,然后这个类帮你在后台加载资源了,一开始会有一个进度条的。但是实际的使用情况发现,这个貌似不好用,进度一直停在1%了,没有动态的加载效果,而且会出现一个黑屏,以及一些残缺的图形显示等。但是当资源加载完成基本上还没有问题的。


import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.*;
import org.andengine.util.progress.IProgressListener;

public class Test extends SimpleAsyncGameActivity{

	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreateResourcesAsync(IProgressListener arg0) throws Exception {
		// TODO Auto-generated method stub
		//加载资源
	}

	@Override
	public Scene onCreateSceneAsync(IProgressListener arg0) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onPopulateSceneAsync(Scene arg0, IProgressListener arg1)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}


第三种:是在官方的用户组里面找到的:


import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class Test extends SimpleBaseGameActivity {
        // ===========================================================
        // Constants
        // ===========================================================

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

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

        // ===========================================================
        // Constructors
        // ===========================================================

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================

        @Override
        public EngineOptions onCreateEngineOptions() {
                final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

                final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
                engineOptions.getAudioOptions().setNeedsSound(true);

                return engineOptions;
        }
        @Override
        public void onCreateResources()
        {
                loadingTexture();
        }
        @Override
        public Scene onCreateScene()
        {
                this.runOnUiThread(new Runnable() {
                  public void run(){
                          IAsyncCallback callback = new IAsyncCallback() {
                                 @Override
                                 public void workToDo() {
                                         loadOtherTexture();                  
                                 }
                                 @Override
                                 public void onComplete() {
                                         creatMenuScene();
                                 }
                          };
                      new AsyncTaskLoader().execute(callback);
                  }
                });
                return creatLoadingScene();
        }
        public void loadingTexture()
        {
                //加载loading资源
        }
        public void loadOtherTexture()
        {
                //加载其他的资源
        }
         /**建立menuScene*/
        public Scene creatLoadingScene()
        {
                final Scene scene = new Scene();
                return scene;
        }
        /**建立menuScene*/
        public Scene creatMenuScene()
        {

        }
}


用到的两个其他的自定的类:


public abstract class IAsyncCallback {
    // ===========================================================
    // Methods
    // ===========================================================
    public abstract void workToDo();

    public abstract void onComplete();
}
public class AsyncTaskLoader extends AsyncTask<IAsyncCallback, Integer, Boolean> {

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

    IAsyncCallback[] _params;

    // ===========================================================
    // Inherited Methods
    // ===========================================================

    @Override
    protected Boolean doInBackground(IAsyncCallback... params) {
        this._params = params;
        int count = params.length;
        for(int i = 0; i < count; i++){
            params[i].workToDo();
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        int count = this._params.length;
        for(int i = 0; i < count; i++){
            this._params[i].onComplete();
        }
    }
}


目前使用的的是这个,感觉不错,但是也有问题,就是在后台加载完资源的显示下一个场景的时候会显示一个灰色的屏,貌似不是每次都显示的。

具体的可以参考用户组的这篇帖子:

http://www.andengine.org/forums/gles2/problems-with-asynctaskloader-in-gles2-in-oncreatescene-t6250.html

总结:

自己刚开始做java方面的东西,有很多的不懂。发现andEngine的中文资源太少了,有太多的相同的帖子,还是谷歌的适合技术贴的搜索。

发现LGame游戏框架的作者的这篇文章的不错,但就是没有发现发现作者类似的文章了。

http://blog.csdn.net/cping1982/article/details/6227775

作者在Android游戏框架AndEngine使用入门这篇文章也谈到资源加载的东西,但是貌似那个方法的再新的版本上无法正常运行了,一运行便出现错误了。




转载于:https://my.oschina.net/u/134408/blog/83796

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值