0

我正在与 AndEngine 一起构建我的游戏...我正在创建菜单屏幕,其中至少有 4 个垂直设置的菜单项。

第 1、2 和 4 项运行良好,但是当它(在屏幕上)单击指向 id 2 的 menuItem onclick 侦听器时,第 3 项出现问题,并且动画精灵/图像也转到第 2 项。

这是我的代码

菜单项 ID

    private MenuScene menuChildScene;
    private final int SINGLE_GAME = 0;
    private final int MULTI_GAME = SINGLE_GAME+ 1;
    private final int HIGH_SCORE = MULTI_GAME + 1;
    private final int ABOUT = HIGH_SCORE + 1;

创建菜单项的东西

private void createMenuChildScene()
    {
        menuChildScene = new MenuScene(camera);
        menuChildScene.setPosition(0,0);

        final IMenuItem opNewgame = new ScaleMenuItemDecorator(new SpriteMenuItem(SINGLE_GAME, resourcesManager.btnSinglegame, vbom), 1.2f, 1);
        final IMenuItem opMultiGame = new ScaleMenuItemDecorator(new SpriteMenuItem(MULTI_GAME, resourcesManager.btnMultiPlayergame, vbom), 1.2f, 1);
        final IMenuItem opHightScore = new ScaleMenuItemDecorator(new SpriteMenuItem(HIGH_SCORE, resourcesManager.btnhighscore, vbom), 1.2f, 1);
        final IMenuItem opAbout= new ScaleMenuItemDecorator(new SpriteMenuItem(ABOUT, resourcesManager.btnabout, vbom), 1.2f, 1);

        menuChildScene.addMenuItem(opNewgame);
        menuChildScene.addMenuItem(opMultiGame);
        menuChildScene.addMenuItem(opHightScore);
        menuChildScene.addMenuItem(opAbout);

        menuChildScene.buildAnimations();
        menuChildScene.setBackgroundEnabled(false);
        opNewgame.setPosition(opNewgame.getX(), opNewgame.getY() - 25);
        opMultiGame.setPosition(opMultiGame.getX(), opNewgame.getY() - 105);
        opHightScore.setPosition(opHightScore.getX(), opMultiGame.getY() - 105);
        opAbout.setPosition(opAbout.getX(), opHightScore.getY() - 105);
        menuChildScene.setOnMenuItemClickListener(this);

        setChildScene(menuChildScene);
    }

onlick 句柄和监听器..

@Override
    public boolean onMenuItemClicked(
            org.andengine.entity.scene.menu.MenuScene pMenuScene,
            IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY) { Log.i(TAG, HIGH_SCORE +  " click : " + pMenuItem.getID());
        // TODO Auto-generated method stub
        switch(pMenuItem.getID()){ 
            case SINGLE_GAME:
                resourcesManager.click.play();
                SceneManager.getInstance().loadGameScene(engine);
                return true;
            case MULTI_GAME: 
                resourcesManager.click.play();
                return true;
            case HIGH_SCORE: 
                resourcesManager.click.play();
                return true;
            case ABOUT:
                resourcesManager.click.play();
                return true;
            default:
                return false;
        }
    }

请帮助我必须更改一个以使我的所有菜单正常工作..

谢谢,最好的问候

4

2 回答 2

1

I just had the same problem.

From what I can tell, in SOME cases the ItemMenus respond to their original positions where they are first being placed. Hence, if you remove the setPosition from each itemMenu, and then try to click your items, it should work perfectly fine.

I've got a code which looks almost just like your :

targetMenuScene = new MenuScene(camera);
targetMenuScene.setPosition(500, -1000);
targetMenuScene.attachChild(targetBackground);
targetMenuScene.setBackgroundEnabled(false);
targetMenuScene.setOnMenuItemClickListener(this);

xMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_X,
           resourcesManager.x, vbom), 2.1f, 2f);
xMenuItem.setPosition(920, -62);
targetMenuScene.addMenuItem(xMenuItem);

... (here are many more menuItems being added....)

setChildScene(targetMenuScene);

This code works absolutly great. On the other hand, I just copied this code to a different place in my program, and I'm getting the same problem just as you do!

What I did notice is, If I alternate the code to be as :

subLevelSelectMenu.buildAnimations();
subLevelSelectMenu.addMenuItem(levels_level1);
subLevelSelectMenu.addMenuItem(levels_level2);
subLevelSelectMenu.addMenuItem(levels_level3);
subLevelSelectMenu.addMenuItem(levels_level4);
subLevelSelectMenu.addMenuItem(levels_level5);

(By that I mean, first giving them their locations (positions), second using buildAnimations, and at last, adding them to the menuScene)....

It works perfect now. I dont understand why, and not sure what BuildAnimations() actually does, but I hope this will help you out a bit.

** Notice that at my first example I didn't even use BuildAnimations(). but I was only able to fix my problem at a different part of my game with it.

If anyone else can share some information regarding this problem, please do so.

EDIT:

after trying some other stuff out, I simple used

subLevelSelectMenu.clearTouchAreas();

before adding all the menu items... and hey, that worked!

good luck!

于 2013-10-03T20:03:06.670 回答
0

再次检查菜单项的位置。通常,您不必设置它们的位置,它们是一个接一个垂直放置的。我认为您的 2 个菜单项相互重叠。尽量不要为菜单项设置位置。它们将被自动设置。

于 2013-06-27T19:40:45.917 回答