0

我是 AndEngine 的新手,我正在尝试使用一个菜单创建一个场景,其中只有一个按钮项,其中仅包含一个标签。

这是代码

public class TestActivity extends SimpleBaseGameActivity implements IOnMenuItemClickListener
{
    static final int CAMERA_WIDTH = 800;
    static final int CAMERA_HEIGHT = 480;
    Camera mCamera;
    @Override
    public EngineOptions onCreateEngineOptions()
    {
        mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true,
                                 ScreenOrientation.LANDSCAPE_SENSOR,
                                 new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
                                 mCamera);
    }

    @Override
    protected void onCreateResources()
    {
        // TODO Auto-generated method stub
    }

    @Override
    protected Scene onCreateScene()
    {
        //Scene scene = new Scene();
        //scene.setBackground(new Background(0.09804f, 0.6274f, 0));
        //Entity layer = new Entity();
        //scene.attachChild(layer);

        Font mFont = FontFactory.create(this.getFontManager(),
                                        this.getTextureManager(),
                                        256, 256,
                                        Typeface.create(Typeface.DEFAULT, Typeface.BOLD),
                                        32);
        mFont.load();

        //Text centerText = new Text(100, 40, mFont, "Hello AndEngine!\nYou can even have multilined text!", new TextOptions(HorizontalAlign.CENTER), this.getVertexBufferObjectManager());
        //layer.attachChild(centerText);

        TextMenuItem playText = new TextMenuItem(1, mFont, "TEXT_TO_DISPLAY",
                                                 this.getVertexBufferObjectManager());
        MenuScene ms = new MenuScene(); 
        ms.addMenuItem(playText);
        ms.setBackground(new Background(0.09804f, 0.6274f, 0));
        //ms.buildAnimations();
        //ms.setBackgroundEnabled(false);

        ms.setOnMenuItemClickListener(this);
        //scene.attachChild(ms);

        return ms;
    }

    @Override
    public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
                                     float pMenuItemLocalX, float pMenuItemLocalY)
    {
        // TODO Auto-generated method stub
        return false;
    }
}

应用程序崩溃,我不知道如何修复它

这是日志猫堆栈跟踪

11-09 07:57:17.670: E/dalvikvm(3960): GC_EXPLICIT freed 186K, 42% free 8192K/13955K, paused 21ms+64ms, total 212ms
11-09 07:57:17.900: E/dalvikvm(3960): GC_EXPLICIT freed 68K, 42% free 8172K/13955K, paused 118ms+33ms, total 179ms
11-09 07:57:17.910: W/dalvikvm(3960): threadid=13: thread exiting with uncaught exception (group=0x4187c438)

我注意到如果我取消注释 buildAnimations() 我会得到一个 nullPointer 异常

4

1 回答 1

4

在 AndEngine 中,MenuScene扩展了CameraScene, 为了使其CameraScene正常工作,它需要一个对 Camera 对象的引用。

如果使用默认构造函数创建 MenuScene 对象,则需要setCamera()在 AndEngine 开始更新场景之前设置 Camera 对象(通过方法)(这将需要有效的相机对象)。或者您可以直接使用带有 Camera 参数的其他构造函数。

于 2013-11-11T14:08:54.133 回答