AndEngine MenuScene - 无法点击按钮

时间:2014-06-16 18:39:23

标签: android andengine

我对android&有一个小问题andengine。 这是我的主菜单的源代码

// AbstractScene is extending Scene
public class MainMenuScene extends AbstractScene implements
        IOnMenuItemClickListener {
    private static final int MENU_CREATE = 10;
    private static final int MENU_JOIN = 20;
    private static final int MENU_ABOUT = 30;
    private static final int MENU_SETTINGS = 40;

    MenuScene scene;

    @Override
    public void loadResources() {
        res.loadMenuResources();
    }

    @Override
    public void create() {
        // Background
        setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
        // Buttons
        scene = new MenuScene(res.camera);
        //scene.setPosition(GameActivity.WIDTH_CENTER, GameActivity.HEIGHT_CENTER);

        final SpriteMenuItem createGameMenuItem = new SpriteMenuItem(
                MENU_CREATE, res.createRegion, res.vbom);
        final SpriteMenuItem joinGameMenuItem = new SpriteMenuItem(MENU_JOIN,
                res.joinRegion, res.vbom);
        final SpriteMenuItem aboutMenuItem = new SpriteMenuItem(MENU_ABOUT,
                res.aboutRegion, res.vbom);
        final SpriteMenuItem settingsMenuItem = new SpriteMenuItem(
                MENU_SETTINGS, res.settingsRegion, res.vbom);

        scene.addMenuItem(createGameMenuItem);
        scene.addMenuItem(joinGameMenuItem);
        scene.addMenuItem(aboutMenuItem);
        scene.addMenuItem(settingsMenuItem);

        scene.buildAnimations();
        scene.setBackgroundEnabled(false);
        scene.setOnMenuItemClickListener(this);
        setChildScene(scene, false, true, true);
    }


    @Override
    public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
            float pMenuItemLocalX, float pMenuItemLocalY) {
        Log.d("Menuitem", "click");
        switch (pMenuItem.getID()) {
        case MENU_CREATE:
            return true;
        case MENU_JOIN:
            SceneManager.getInstance().showScene(SceneType.LISTGAMES);
            return true;
        case MENU_ABOUT:
            return true;
        case MENU_SETTINGS:
            return true;
        default:
            return false;
        }
    }
}

这是我在ResourceManager中的代码

// menu ressources
public void loadMenuResources() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    menuAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(),
            1024, 1024, BitmapTextureFormat.RGBA_8888, TextureOptions.BILINEAR);
    createRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(menuAtlas, activity.getAssets(), "btn_create_game.png");
    joinRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(menuAtlas, activity.getAssets(), "btn_join_game.png");
    settingsRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(menuAtlas, activity.getAssets(), "btn_settings.png");
    aboutRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(menuAtlas, activity.getAssets(), "btn_about.png");

    try {
        menuAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
        menuAtlas.load();
    } catch (TextureAtlasBuilderException e) {
        throw new RuntimeException("Error while loading menu resources", e);
    }
}

我的问题是,OnMenuItemClickListener没有被调用,我不知道为什么。大多数代码都是从官方示例中复制而来的。我的应用程序显示了背景菜单按钮等,但我无法点击其中一个。

有人知道为什么吗? THX!

1 个答案:

答案 0 :(得分:-1)

您缺少注册区触摸区

 scene.registerTouchArea(createGameMenuItem);
 scene.registerTouchArea(joinGameMenuItem);
 scene.registerTouchArea(aboutMenuItem);
 scene.registerTouchArea(settingsMenuItem);