2013-03-20 159 views
0

我是使用andengine進行android遊戲開發的新手。我正嘗試創建一個場景,該場景將包含一個靜態樹和一個可以在某些控件的幫助下移動和控制的播放器。我創建了一個正在移動的播放器,也可以用一些控制器進行控制,但是現在當我添加一個靜態樹時,它會給出一個錯誤。在Android遊戲中創建場景

下面是代碼和錯誤的logcat:

package se.treplex.sketchytruck; 

import javax.microedition.khronos.opengles.GL10; 

import org.anddev.andengine.engine.Engine; 
import org.anddev.andengine.engine.camera.Camera; 
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl; 
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl; 
import org.anddev.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener; 
import org.anddev.andengine.engine.handler.physics.PhysicsHandler; 
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.modifier.ScaleModifier; 
import org.anddev.andengine.entity.modifier.SequenceEntityModifier; 
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.opengl.texture.TextureOptions; 
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; 
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; 
import org.anddev.andengine.opengl.texture.region.TextureRegion; 

import android.widget.Toast; 

public class demo1 extends BaseExample{ 
    private static final int CAMERA_WIDTH = 600; 
    private static final int CAMERA_HEIGHT = 800; 

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

    private Camera mCamera; 

    private BitmapTextureAtlas mBitmapTextureAtlas; 
    private TextureRegion mFaceTextureRegion; 

    private BitmapTextureAtlas mOnScreenControlTexture,treeatlas; 
    private TextureRegion mOnScreenControlBaseTextureRegion,treergn; 
    private TextureRegion mOnScreenControlKnobTextureRegion; 

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

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

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

    @Override 
    public Engine onLoadEngine() { 
     Toast.makeText(this, "Also try tapping this AnalogOnScreenControl!", 
       Toast.LENGTH_LONG).show(); 
     this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 
     return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, 
       new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), 
       this.mCamera)); 
    } 

    @Override 
    public void onLoadResources() { 
     BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

     this.mBitmapTextureAtlas = new BitmapTextureAtlas(32, 32, 
       TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
     this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory 
       .createFromAsset(this.mBitmapTextureAtlas, this, 
         "face.png", 0, 0); 

     this.mOnScreenControlTexture = new BitmapTextureAtlas(256, 128, 
       TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
     this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory 
       .createFromAsset(this.mOnScreenControlTexture, this, 
         "onscreen_control_base.png", 0, 0); 


     this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory 
       .createFromAsset(this.mOnScreenControlTexture, this, 
         "onscreen_control_knob.png", 128, 0); 

     this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas, 
       this.mOnScreenControlTexture); 

     //for tree 
       this.treeatlas = new BitmapTextureAtlas(1024, 1024, 
         TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
       this.treergn = BitmapTextureAtlasTextureRegionFactory 
         .createFromAsset(this.mOnScreenControlTexture, this, 
           "gfx/treedemo.png", 0, 0); 
       this.mEngine.getTextureManager().loadTexture(treeatlas); 
    } 

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

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

     //for tree 
     final Sprite treesprt = new Sprite(10, 0, 
       this.mFaceTextureRegion); 


     scene.attachChild(treesprt); 


     final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth())/2; 
     final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion 
       .getHeight())/2; 
     final Sprite face = new Sprite(centerX, centerY, 
       this.mFaceTextureRegion); 
     final PhysicsHandler physicsHandler = new PhysicsHandler(face); 
     face.registerUpdateHandler(physicsHandler); 

     scene.attachChild(face); 



     final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(
       0, CAMERA_HEIGHT 
         - this.mOnScreenControlBaseTextureRegion.getHeight(), 
       this.mCamera, this.mOnScreenControlBaseTextureRegion, 
       this.mOnScreenControlKnobTextureRegion, 0.1f, 200, 
       new IAnalogOnScreenControlListener() { 
        @Override 
        public void onControlChange(
          final BaseOnScreenControl pBaseOnScreenControl, 
          final float pValueX, final float pValueY) { 
         physicsHandler 
           .setVelocity(pValueX * 100, pValueY * 100); 
        } 

        @Override 
        public void onControlClick(
          final AnalogOnScreenControl pAnalogOnScreenControl) { 
         face.registerEntityModifier(new SequenceEntityModifier(
           new ScaleModifier(0.25f, 1, 1.5f), 
           new ScaleModifier(0.25f, 1.5f, 1))); 
        } 
       }); 
     analogOnScreenControl.getControlBase().setBlendFunction(
       GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
     analogOnScreenControl.getControlBase().setAlpha(0.5f); 
     analogOnScreenControl.getControlBase().setScaleCenter(0, 128); 
     analogOnScreenControl.getControlBase().setScale(1.25f); 
     analogOnScreenControl.getControlKnob().setScale(1.25f); 
     analogOnScreenControl.refreshControlKnobPosition(); 

     scene.setChildScene(analogOnScreenControl); 

     return scene; 
    } 

    @Override 
    public void onLoadComplete() { 

    } 
} 

的logcat:

03-20 14:40:42.343: E/AndEngine(25980): Failed loading Bitmap in AssetBitmapTextureAtlasSource. AssetPath: gfx/face.png 
03-20 14:40:42.343: E/AndEngine(25980): java.io.FileNotFoundException: gfx/face.png 
03-20 14:40:42.343: E/AndEngine(25980):  at android.content.res.AssetManager.openAsset(Native Method) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.content.res.AssetManager.open(AssetManager.java:314) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.content.res.AssetManager.open(AssetManager.java:288) 
03-20 14:40:42.343: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource.<init>(AssetBitmapTextureAtlasSource.java:55) 
03-20 14:40:42.343: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource.<init>(AssetBitmapTextureAtlasSource.java:42) 
03-20 14:40:42.343: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory.createFromAsset(BitmapTextureAtlasTextureRegionFactory.java:75) 
03-20 14:40:42.343: E/AndEngine(25980):  at se.treplex.sketchytruck.demo1.onLoadResources(demo1.java:73) 
03-20 14:40:42.343: E/AndEngine(25980):  at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:168) 
03-20 14:40:42.343: E/AndEngine(25980):  at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85) 
03-20 14:40:42.343: E/AndEngine(25980):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2103) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.view.View.dispatchWindowFocusChanged(View.java:3924) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:659) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1991) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.os.Looper.loop(Looper.java:130) 
03-20 14:40:42.343: E/AndEngine(25980):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
03-20 14:40:42.343: E/AndEngine(25980):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-20 14:40:42.343: E/AndEngine(25980):  at java.lang.reflect.Method.invoke(Method.java:507) 
03-20 14:40:42.343: E/AndEngine(25980):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
03-20 14:40:42.343: E/AndEngine(25980):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
03-20 14:40:42.343: E/AndEngine(25980):  at dalvik.system.NativeStart.main(Native Method) 
03-20 14:40:42.367: E/AndEngine(25980): Failed loading Bitmap in AssetBitmapTextureAtlasSource. AssetPath: gfx/gfx/treedemo.png 
03-20 14:40:42.367: E/AndEngine(25980): java.io.FileNotFoundException: gfx/gfx/treedemo.png 
03-20 14:40:42.367: E/AndEngine(25980):  at android.content.res.AssetManager.openAsset(Native Method) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.content.res.AssetManager.open(AssetManager.java:314) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.content.res.AssetManager.open(AssetManager.java:288) 
03-20 14:40:42.367: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource.<init>(AssetBitmapTextureAtlasSource.java:55) 
03-20 14:40:42.367: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource.<init>(AssetBitmapTextureAtlasSource.java:42) 
03-20 14:40:42.367: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory.createFromAsset(BitmapTextureAtlasTextureRegionFactory.java:75) 
03-20 14:40:42.367: E/AndEngine(25980):  at se.treplex.sketchytruck.demo1.onLoadResources(demo1.java:94) 
03-20 14:40:42.367: E/AndEngine(25980):  at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:168) 
03-20 14:40:42.367: E/AndEngine(25980):  at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85) 
03-20 14:40:42.367: E/AndEngine(25980):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2103) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.view.View.dispatchWindowFocusChanged(View.java:3924) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:659) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1991) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.os.Looper.loop(Looper.java:130) 
03-20 14:40:42.367: E/AndEngine(25980):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
03-20 14:40:42.367: E/AndEngine(25980):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-20 14:40:42.367: E/AndEngine(25980):  at java.lang.reflect.Method.invoke(Method.java:507) 
03-20 14:40:42.367: E/AndEngine(25980):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
03-20 14:40:42.367: E/AndEngine(25980):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
03-20 14:40:42.367: E/AndEngine(25980):  at dalvik.system.NativeStart.main(Native Method) 
03-20 14:40:42.687: D/libEGL(25980): loaded /system/lib/egl/libGLES_hgl.so 
03-20 14:40:42.757: E/(25980): Creating OpenGL 1.1 Session 
03-20 14:40:42.796: D/BRCM_EGL(25980): eglCreateContext() context: 0x1b7618, VC context 1, Thread 26006 
03-20 14:40:42.796: D/BRCM_EGL(25980): eglCreateWindowSurface() surface: 0x1dfcd0, VC surface: 1, Thread: 26006 
03-20 14:40:42.796: D/BRCM_EGL(25980): eglMakeCurrent(0x1b7618, 0x1dfcd0, 0x1dfcd0) Thread: 26006 
03-20 14:40:42.828: D/AndEngine(25980): onSurfaceCreated 
03-20 14:40:42.851: D/AndEngine(25980): RENDERER: VideoCore IV HW 
03-20 14:40:42.851: D/AndEngine(25980): VERSION: OpenGL ES-CM 1.1 
03-20 14:40:42.851: D/AndEngine(25980): EXTENSIONS: GL_OES_compressed_ETC1_RGB8_texture GL_OES_compressed_paletted_texture GL_OES_texture_npot GL_OES_EGL_image GL_EXT_discard_framebuffer GL_OES_query_matrix GL_OES_framebuffer_object GL_OES_rgb8_rgba8 GL_OES_depth24 GL_OES_depth32 GL_OES_stencil8 GL_OES_draw_texture 
03-20 14:40:42.851: D/AndEngine(25980): EXTENSIONS_VERXTEXBUFFEROBJECTS = true 
03-20 14:40:42.859: D/AndEngine(25980): EXTENSIONS_DRAWTEXTURE = true 
03-20 14:40:42.859: D/AndEngine(25980): onSurfaceChanged: pWidth=240 pHeight=320 
03-20 14:40:42.882: E/AndEngine(25980): Failed loading Bitmap in AssetBitmapTextureAtlasSource. AssetPath: gfx/face.png 
03-20 14:40:42.882: E/AndEngine(25980): java.io.FileNotFoundException: gfx/face.png 
03-20 14:40:42.882: E/AndEngine(25980):  at android.content.res.AssetManager.openAsset(Native Method) 
03-20 14:40:42.882: E/AndEngine(25980):  at android.content.res.AssetManager.open(AssetManager.java:314) 
03-20 14:40:42.882: E/AndEngine(25980):  at android.content.res.AssetManager.open(AssetManager.java:288) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource.onLoadBitmap(AssetBitmapTextureAtlasSource.java:105) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.writeTextureToHardware(BitmapTextureAtlas.java:156) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.Texture.loadToHardware(Texture.java:116) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:146) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:503) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:154) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:617) 
03-20 14:40:42.882: E/AndEngine(25980):  at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.run(GLSurfaceView.java:549) 
03-20 14:40:42.890: E/AndEngine(25980): Error loading: AssetBitmapTextureAtlasSource(gfx/face.png) 
03-20 14:40:42.890: E/AndEngine(25980): java.lang.IllegalArgumentException: AssetBitmapTextureAtlasSource: AssetBitmapTextureAtlasSource(gfx/face.png) returned a null Bitmap. 
03-20 14:40:42.890: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.writeTextureToHardware(BitmapTextureAtlas.java:159) 
03-20 14:40:42.890: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.Texture.loadToHardware(Texture.java:116) 
03-20 14:40:42.890: E/AndEngine(25980):  at org.anddev.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:146) 
03-20 14:40:42.890: E/AndEngine(25980):  at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:503) 
03-20 14:40:42.890: E/AndEngine(25980):  at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:154) 
03-20 14:40:42.890: E/AndEngine(25980):  at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:617) 
03-20 14:40:42.890: E/AndEngine(25980):  at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.run(GLSurfaceView.java:549) 
03-20 14:40:42.890: W/dalvikvm(25980): threadid=10: thread exiting with uncaught exception (group=0x40018578) 
03-20 14:40:42.914: E/AndroidRuntime(25980): FATAL EXCEPTION: GLThread 
03-20 14:40:42.914: E/AndroidRuntime(25980): java.lang.IllegalArgumentException: AssetBitmapTextureAtlasSource: AssetBitmapTextureAtlasSource(gfx/face.png) returned a null Bitmap. 
03-20 14:40:42.914: E/AndroidRuntime(25980): at org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.writeTextureToHardware(BitmapTextureAtlas.java:159) 
03-20 14:40:42.914: E/AndroidRuntime(25980): at org.anddev.andengine.opengl.texture.Texture.loadToHardware(Texture.java:116) 
03-20 14:40:42.914: E/AndroidRuntime(25980): at org.anddev.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:146) 
03-20 14:40:42.914: E/AndroidRuntime(25980): at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:503) 
03-20 14:40:42.914: E/AndroidRuntime(25980): at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:154) 
03-20 14:40:42.914: E/AndroidRuntime(25980): at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:617) 
03-20 14:40:42.914: E/AndroidRuntime(25980): at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.run(GLSurfaceView.java:549) 
03-20 14:40:43.046: D/dalvikvm(25980): GC_CONCURRENT freed 297K, 49% free 2927K/5639K, external 417K/517K, paused 3ms+4ms 
+0

你忘了加上logcat的錯誤。 – 2013-03-20 10:12:36

+0

感謝michal ..現在我添加了logcat錯誤...請檢查它,如果有一些解決方案,然後好心告訴我PLZ .... – user2190151 2013-03-20 11:14:25

+0

無法在AssetBitmapTextureAtlasSource中加載位圖。 AssetPath:gfx/face.png – 2013-03-20 12:00:03

回答

0

一切都在錯誤說:

java.io.FileNotFoundException: gfx/face.png 

方法createFromAsset(...)無法找到圖片。

你確定這是正確的路徑(gfx/face.png)訪問face.png?這張照片是gfx

+0

是的,圖片在那裏...... – user2190151 2013-03-20 16:41:35

+0

其實這個項目已經有兩個機構。我想添加第三個靜態的身體..所以當我添加那個身體那裏它會得到錯誤...如果你知道任何其他的解決方案,然後可以告訴我。謝謝 – user2190151 2013-03-20 16:43:49

0
  1. image face.png不存在assets/gfx文件夾中。在logcat中清晰地提到。
  2. 一旦你設定GFX作爲資產

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
    

    一個基本路徑則沒有必要GFX /寫一遍代碼中即將推出的線,你在這裏寫這篇文章。所以從下面的代碼中刪除gfx /。

    this.treergn = BitmapTextureAtlasTextureRegionFactory 
           .createFromAsset(this.mOnScreenControlTexture, this, 
             "gfx/treedemo.png", 0, 0);