Level selector scene would be same as previously created man menu scene only the difference is buttons we are using.
So lets have a look at the graphics we are using,
lmenuback,png
package com.mindew.fruitecollector;
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.util.GLState;
import org.andengine.engine.camera.*;
import com.mindew.fruitecollector.SceneManager.SceneType;
public class LevelMenuScene extends BaseScene implements IOnMenuItemClickListener{
private MenuScene menuChildScene;
private final int MENU_LONE = 0;
private final int MENU_LTWO = 1;
private final int MENU_LTHREE = 2;
private final int MENU_LFOUR = 3;
private final int MENU_LFIVE = 4;
@Override
public void createScene() {
// TODO Auto-generated method stub
createBackground();
createMenuChildScene();
}
@Override
public void onBackKeyPressed() {
// TODO Auto-generated method stub
disposeScene();
SceneManager.getInstance().loadMenuScene(engine);
}
@Override
public SceneType getSceneType() {
// TODO Auto-generated method stub
return SceneType.SCENE_LMENU;
}
@Override
public void disposeScene() {
// TODO Auto-generated method stub
}
private void createBackground()
{
attachChild(new Sprite(0, 0, resourcesManager.lmenu_background_region, vbom)
{
@Override
protected void preDraw(GLState pGLState, Camera pCamera)
{
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
});
}
private void createMenuChildScene()
{
menuChildScene = new MenuScene(camera);
menuChildScene.setPosition(0, 0);
final IMenuItem loneMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LONE, resourcesManager.lone_region, vbom), 1.2f, 1);
final IMenuItem ltwoMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LTWO, resourcesManager.ltwo_region, vbom), 1.2f, 1);
final IMenuItem lthreeMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LTHREE, resourcesManager.lthree_region, vbom), 1.2f, 1);
final IMenuItem lfourMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LFOUR, resourcesManager.lfour_region, vbom), 1.2f, 1);
final IMenuItem lfiveMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LFIVE, resourcesManager.lfive_region, vbom), 1.2f, 1);
menuChildScene.addMenuItem(loneMenuItem);
menuChildScene.addMenuItem(ltwoMenuItem);
menuChildScene.addMenuItem(lthreeMenuItem);
menuChildScene.addMenuItem(lfourMenuItem);
menuChildScene.addMenuItem(lfiveMenuItem);
menuChildScene.buildAnimations();
menuChildScene.setBackgroundEnabled(false);
loneMenuItem.setPosition(100,120);
ltwoMenuItem.setPosition(220,120);
lthreeMenuItem.setPosition(340,120);
lfourMenuItem.setPosition(460,120);
lfiveMenuItem.setPosition(580,120);
menuChildScene.setOnMenuItemClickListener(this);
setChildScene(menuChildScene);
}
@Override
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
float pMenuItemLocalX, float pMenuItemLocalY) {
// TODO Auto-generated method stub
switch(pMenuItem.getID())
{
case MENU_LONE:
//SceneManager.getInstance().loadGameScene(engine);
return true;
case MENU_LTWO:
//SceneManager.getInstance().loadLevelTwoScene(engine);
return true;
case MENU_LTHREE:
//SceneManager.getInstance().loadLevelThreeScene(engine);
return true;
case MENU_LFOUR:
//SceneManager.getInstance().loadLevelFourScene(engine);
return true;
case MENU_LFIVE:
//SceneManager.getInstance().loadLevelFiveScene(engine);
return true;
default:
return false;
}
}
}
Now go back to MainMenuScene class and navigate to Override method onMenuItemClicked, remove the comment from line SceneManager.getInstance().createLMenuScene();
So lets have a look at the graphics we are using,
lmenuback,png
lone.png
You can create your own png files for your project, so go ahead and create other buttons for level 2 to 5 and name it as ltwo.png, lthree.png etc.
Now go to resources manager class and initialize textures as below,
public ITextureRegion lmenu_background_region;
public ITextureRegion lone_region;
public ITextureRegion ltwo_region;
public ITextureRegion lthree_region;
public ITextureRegion lfour_region;
public ITextureRegion lfive_region;
private BuildableBitmapTextureAtlas lmenuTextureAtlas;
create a method as below,
private void loadLMenuGraphics()
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
lmenuTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
lmenu_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "lmenuback.png");
lone_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "lone.png");
ltwo_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "ltwo.png");
lthree_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "lthree.png");
lfour_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "lfour.png");
lfive_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "lfive.png");
levelfail_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "levelfail.png");
yes_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "yes.png");
no_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(lmenuTextureAtlas, activity, "no.png");
try
{
this.lmenuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
this.lmenuTextureAtlas.load();
}
catch (final TextureAtlasBuilderException e)
{
Debug.e(e);
}
}
We are done loading the graphics for our level selector scene.
Now we need to define and initialize level selector scene in SceneManager class, go to scene manager class and follow below steps:
> Create variable,
private BaseScene lmenuScene;
> Inside enum SceneType create,
SCENE_LMENU
>Inside method setScene(SceneType sceneType) create case,
case SCENE_LMENU:
setScene(lmenuScene);
break;
>Create a new method as below,
public void createLMenuScene()
{
ResourcesManager.getInstance().loadMenuResources();
lmenuScene = new LevelMenuScene();
loadingScene = new LoadingScene();
setScene(lmenuScene);
}
Done
Ignore the errors for now, we gonna create the new class called LevelMenuScene as below,package com.mindew.fruitecollector;
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.util.GLState;
import org.andengine.engine.camera.*;
import com.mindew.fruitecollector.SceneManager.SceneType;
public class LevelMenuScene extends BaseScene implements IOnMenuItemClickListener{
private MenuScene menuChildScene;
private final int MENU_LONE = 0;
private final int MENU_LTWO = 1;
private final int MENU_LTHREE = 2;
private final int MENU_LFOUR = 3;
private final int MENU_LFIVE = 4;
@Override
public void createScene() {
// TODO Auto-generated method stub
createBackground();
createMenuChildScene();
}
@Override
public void onBackKeyPressed() {
// TODO Auto-generated method stub
disposeScene();
SceneManager.getInstance().loadMenuScene(engine);
}
@Override
public SceneType getSceneType() {
// TODO Auto-generated method stub
return SceneType.SCENE_LMENU;
}
@Override
public void disposeScene() {
// TODO Auto-generated method stub
}
private void createBackground()
{
attachChild(new Sprite(0, 0, resourcesManager.lmenu_background_region, vbom)
{
@Override
protected void preDraw(GLState pGLState, Camera pCamera)
{
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
});
}
private void createMenuChildScene()
{
menuChildScene = new MenuScene(camera);
menuChildScene.setPosition(0, 0);
final IMenuItem loneMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LONE, resourcesManager.lone_region, vbom), 1.2f, 1);
final IMenuItem ltwoMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LTWO, resourcesManager.ltwo_region, vbom), 1.2f, 1);
final IMenuItem lthreeMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LTHREE, resourcesManager.lthree_region, vbom), 1.2f, 1);
final IMenuItem lfourMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LFOUR, resourcesManager.lfour_region, vbom), 1.2f, 1);
final IMenuItem lfiveMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LFIVE, resourcesManager.lfive_region, vbom), 1.2f, 1);
menuChildScene.addMenuItem(loneMenuItem);
menuChildScene.addMenuItem(ltwoMenuItem);
menuChildScene.addMenuItem(lthreeMenuItem);
menuChildScene.addMenuItem(lfourMenuItem);
menuChildScene.addMenuItem(lfiveMenuItem);
menuChildScene.buildAnimations();
menuChildScene.setBackgroundEnabled(false);
loneMenuItem.setPosition(100,120);
ltwoMenuItem.setPosition(220,120);
lthreeMenuItem.setPosition(340,120);
lfourMenuItem.setPosition(460,120);
lfiveMenuItem.setPosition(580,120);
menuChildScene.setOnMenuItemClickListener(this);
setChildScene(menuChildScene);
}
@Override
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
float pMenuItemLocalX, float pMenuItemLocalY) {
// TODO Auto-generated method stub
switch(pMenuItem.getID())
{
case MENU_LONE:
//SceneManager.getInstance().loadGameScene(engine);
return true;
case MENU_LTWO:
//SceneManager.getInstance().loadLevelTwoScene(engine);
return true;
case MENU_LTHREE:
//SceneManager.getInstance().loadLevelThreeScene(engine);
return true;
case MENU_LFOUR:
//SceneManager.getInstance().loadLevelFourScene(engine);
return true;
case MENU_LFIVE:
//SceneManager.getInstance().loadLevelFiveScene(engine);
return true;
default:
return false;
}
}
}
Now go back to MainMenuScene class and navigate to Override method onMenuItemClicked, remove the comment from line SceneManager.getInstance().createLMenuScene();
The above code is similar to the MainMenuScene so I hope it does not need more explanations. run the project and see if you can see your level selector scene.
No comments:
Post a Comment