我是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;
}
}
应用程序崩溃,我无法解决它的问题
这是log cat stack trace
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异常
答案 0 :(得分:4)
在AndEngine中,MenuScene
扩展了CameraScene
,为了使CameraScene
正常工作,需要引用Camera对象。
如果使用默认构造函数创建MenuScene对象,则需要在AndEngine开始更新场景(需要有效的相机对象)之前设置Camera对象(通过setCamera()
方法)。
或者您可以直接使用具有Camera参数的其他构造函数。