在Android游戏中创建场景

时间:2013-03-20 10:06:08

标签: android android-widget

我是使用andengine进行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

2 个答案:

答案 0 :(得分:0)

错误中都说了一切:

java.io.FileNotFoundException: gfx/face.png

方法createFromAsset(...)无法找到图片。

您确定这是访问gfx/face.png的正确途径(face.png)吗?这张照片是gfx吗?

答案 1 :(得分:0)

  1. 图像face.png不存在于assets / gfx文件夹中。在logcat中明确提到。
  2. 将gfx设置为资产的基本路径

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    

    然后就不需要在即将发布的代码行中再次编写gfx /,因为你已经在这里写了这个。所以从下面的代码中删除gfx /。

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