gpt4 book ai didi

java - Android:带有 AndEngine 的 MenuScene

转载 作者:行者123 更新时间:2023-12-01 18:41:22 27 4
gpt4 key购买 nike

我是 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 异常

最佳答案

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

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

关于java - Android:带有 AndEngine 的 MenuScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19873350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com