Positional Menu Animator

If you've wondered how to get your menu wherever you want it instead of centered, then this resource is for you.

It's pretty simple but it does the trick.

Here's the code (you'll have to forgive my variable naming convention, I've been using XNA and C# for the last two years, the underscore is burned in to my habits pretty deep):

import java.util.ArrayList;

import org.anddev.andengine.entity.scene.menu.animator.BaseMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;

public class PositionalMenuAnimator extends BaseMenuAnimator {

    /** 
     * Sets the menu buttons vertically downward starting at position 0,0
     */
    public PositionalMenuAnimator(){
        super();
        this._baseX = 0;
        this._baseY = 0;
    }

    /**
     * Sets the menu buttons vertically downward starting at a set pX,pY
     */
    public PositionalMenuAnimator(final float pX, final float pY){
        super();
        this._baseX = pX;
        this._baseY = pY;
    }

    /**
     * Sets the menu buttons vertically downward starting at a set pX,pY with a set line spacing
     */
    public PositionalMenuAnimator(final float pX, final float pY, final float pLineSpacing){
        super(pLineSpacing);
        this._baseX = pX;
        this._baseY = pY;
    }

    // ===========================================================
    // Fields
    // ===========================================================

    private float _baseX;
    private float _baseY;

    // ===========================================================
    // Inherited Methods
    // ===========================================================

    @Override
    public void prepareAnimations(ArrayList<IMenuItem> pMenuItems,
            float pCameraWidth, float pCameraHeight) {}

    @Override
    public void buildAnimations(ArrayList<IMenuItem> pMenuItems,
            float pCameraWidth, float pCameraHeight) {

        float offsetX = _baseX;
        float offsetY = _baseY;

        final int menuItemCount = pMenuItems.size();
        for(int i = 0; i < menuItemCount; i++) {
            final IMenuItem menuItem = pMenuItems.get(i);

            menuItem.setPosition(offsetX , offsetY);

            offsetY += menuItem.getHeight() + this.mMenuItemSpacing;
        }

    }
}

The constructors are commented and pretty self explanitory.

To use this simply call:

MenuScene menu = new MenuScene(this._engine.getCamera());

        // add some menu items here

        menu.setMenuAnimator(new PositionalMenuAnimator(30,30,30)); // where 30, 30 is the position you want it at and the last 30 is the line spacing you want
        menu.buildAnimations();

Make sure you call setMenuAnimator BEFORE menu.buildAnimations! *

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License