3
votes

I was on a roll last night, when I came across the issue of the ButtonSprite. I could have sworn I've done this before - it seems so simple - but using the code that AndEngineExamples provides doesn't really make sense and comes up with errors (for instance - why does a vague "run()" method create the response to every single touch event?).

So I did something that seemed to make sense.

private void loadScenes()
{
    mainScene = new Scene();
    menu = new Sprite(0, 0, menuTextureRegion, mEngine.getVertexBufferObjectManager()){
        @Override
        protected void preDraw(GLState pGLState, Camera pCamera) 
        {
            super.preDraw(pGLState, pCamera);
            pGLState.enableDither();
        }
    };

    newgamebutton = new ButtonSprite(25, 200, newgameTextureRegion, mEngine.getVertexBufferObjectManager());
    multiplayerbutton = new ButtonSprite(25, 310, multiplayerTextureRegion, mEngine.getVertexBufferObjectManager());

    menu.setScale(1.65f);
    menu.setPosition((CAMERA_WIDTH - menu.getWidth()) * 0.5f, (CAMERA_HEIGHT - menu.getHeight()) * 0.5f);

    mainScene.attachChild(menu);
    mainScene.attachChild(newgamebutton);
    mainScene.attachChild(multiplayerbutton);

    mainScene.registerTouchArea(multiplayerbutton);
    mainScene.setTouchAreaBindingOnActionDownEnabled(true);

    multiplayerbutton.onAreaTouched(mButtonTouched, multiplayerbutton.getWidth(), multiplayerbutton.getHeight());

    if(mButtonTouched.isActionDown()) {
        mainScene.detachChild(menu);
        mainScene.detachChild(multiplayerbutton);
    }

}

It seems to make good sense. When the "multiplayer button" is pressed, it gives you the mButtonTouched, and when that's pressed down... it detaches the menu and multiplayer button. This is obviously just me testing things, and not part of any actual project because I'm just learning, but I get a null pointer exception and the app right closes before displaying the "Menu Scene", which obviously normally works. Also, on various sites it says that I should put "public boolean" before multiplayerbutton, but Eclipse won't have that.

I'm sure you guys have already figured this out, but I'm suuuuuuper new to AndEngine. Thanks for any help, guys!

1
Please add a stack trace or at least tell us what line of code causes problems. - JohnEye
Basically the last four lines - the button refuses to work. - user1591345

1 Answers

4
votes

After looking at the code I believe you wanted to do something like this:

ButtonSprite button = new ButtonSprite(25, 310, multiplayerTextureRegion, mEngine.getVertexBufferObjectManager()) {
       @Override
       public boolean onAreaTouched(TouchEvent pTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
           if(pTouchEvent.isActionDown()) {
               mainScene.detachChild(menu);
               mainScene.detachChild(multiplayerbutton);
           }
           return super.onAreaTouched(pTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
       }
};

Your code doesn't work because the last if statement doesn't really make sense - it is checking whether the button is pressed when the method loadScenes() executes. I suggest you read up on anonymous inner classes, it took me a long time to figure out that using them is a valid and useful approach in AndEngine.

Here is a primitive example of a different way of managing buttons:

mScene.setOnAreaTouchListener(new IOnAreaTouchListener() {
    @Override
    public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) {
        if (pTouchArea.equals(button) {
            doSomething();
            return true;
        }
        return false;
    }
});

Please note that this only works when touching Shapes, if you want to detect touch events outside of Shapes, you need to use IOnSceneTouchListener instead.