Skip to content

Instantly share code, notes, and snippets.

@pratamawijaya
Created October 15, 2013 09:06
Show Gist options
  • Save pratamawijaya/6988814 to your computer and use it in GitHub Desktop.
Save pratamawijaya/6988814 to your computer and use it in GitHub Desktop.
package id.pratama.menututorial;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;
public class GameActivity extends SimpleBaseGameActivity implements IOnMenuItemClickListener
{
private final float WIDTH = 480;
private final float HEIGHT = 320;
private Camera camera;
private Scene mainScene;
private MenuScene menuScene;
private final int MENU_PLAY = 0;
private final int MENU_OPT = 1;
private BuildableBitmapTextureAtlas menuBtnTex;
private ITextureRegion menuBtnPlayReg;
private ITextureRegion menuBtnOptionsReg;
@Override
public EngineOptions onCreateEngineOptions()
{
// TODO Auto-generated method stub
camera = new Camera(0, 0, WIDTH, HEIGHT);
EngineOptions en = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(WIDTH,
HEIGHT), camera);
return en;
}
@Override
protected void onCreateResources()
{
// TODO Auto-generated method stub
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
menuBtnTex = new BuildableBitmapTextureAtlas(getTextureManager(), 1024, 128,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
menuBtnPlayReg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuBtnTex, getAssets(), "play.png");
menuBtnOptionsReg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuBtnTex, getAssets(),
"options.png");
try
{
menuBtnTex.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
menuBtnTex.load();
} catch (TextureAtlasBuilderException e)
{
// TODO: handle exception
}
}
@Override
protected Scene onCreateScene()
{
// TODO Auto-generated method stub
mainScene = new Scene();
mainScene.setBackground(new Background(Color.WHITE));
menuScene = new MenuScene(camera);
final IMenuItem playButton = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_PLAY, 200, 70, menuBtnPlayReg,
getVertexBufferObjectManager()), 1.1f, 1);
final IMenuItem optButton = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_OPT, 200, 70, menuBtnOptionsReg,
getVertexBufferObjectManager()), 1.1f, 1);
playButton.setPosition(WIDTH / 2 - playButton.getWidth() / 2, 100);
optButton.setPosition(WIDTH / 2 - optButton.getWidth() / 2, 200);
menuScene.addMenuItem(playButton);
menuScene.addMenuItem(optButton);
menuScene.buildAnimations();
menuScene.setBackgroundEnabled(false);
menuScene.setOnMenuItemClickListener(this);
mainScene.setChildScene(menuScene);
return mainScene;
}
@Override
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX,
float pMenuItemLocalY)
{
// TODO Auto-generated method stub
switch (pMenuItem.getID())
{
case MENU_PLAY:
// when click play
break;
case MENU_OPT:
// when click options
break;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment