一開始沒有注意資源加載的問題,但後圖形漸漸的增長了,啓動畫面黑屏的時間愈來愈長了,終於直接在加載的資源的時候就掛掉了。 html
加載資源的解決方案,大致搜索了下有這麼幾種: java
第一種:來源於這個網站https://sites.google.com/site/matimdevelopment/splash-screen---easy-way 框架
如下完整代碼的整理: async
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%了,沒有動態的加載效果,並且會出現一個黑屏,以及一些殘缺的圖形顯示等。可是當資源加載完成基本上尚未問題的。 ide
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(); } } }
目前使用的的是這個,感受不錯,可是也有問題,就是在後臺加載完資源的顯示下一個場景的時候會顯示一個灰色的屏,貌似不是每次都顯示的。 ui
具體的能夠參考用戶組的這篇帖子: this
總結:
本身剛開始作java方面的東西,有不少的不懂。發現andEngine的中文資源太少了,有太多的相同的帖子,仍是谷歌的適合技術貼的搜索。
發現LGame遊戲框架的做者的這篇文章的不錯,但就是沒有發現發現做者相似的文章了。
http://blog.csdn.net/cping1982/article/details/6227775
做者在Android遊戲框架AndEngine使用入門這篇文章也談到資源加載的東西,可是貌似那個方法的再新的版本上沒法正常運行了,一運行便出現錯誤了。