* Creating Main Menu Scene
In this post I will be showing you how to create main menu scene. Follow the steps below,
1. First off all lets have a look at graphics below,
menuback.png
In this post I will be showing you how to create main menu scene. Follow the steps below,
1. First off all lets have a look at graphics below,
menuback.png
play.png
scores.png
> play button will navigate you to level selector scene
> scores button will navigate you to high score scene
2. Go to ResourcesManager class and define textures
public ITextureRegion menu_background_region;
public ITextureRegion play_region;
public ITextureRegion scores_region;
private BuildableBitmapTextureAtlas menuTextureAtlas;
Go to load menu graphics and put below code
private void loadMenuGraphics()
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
menuTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
menu_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "menuback.png");
play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "play.png");
scores_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "scores.png");
try
{
this.menuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
this.menuTextureAtlas.load();
}
catch (final TextureAtlasBuilderException e)
{
Debug.e(e);
}
}
We are done with loading assets.
3. Now lets create the new class called MainMenuScene as below,
package com.mindew.fruitecollector;
import org.andengine.audio.music.Music;
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 MainMenuScene extends BaseScene implements IOnMenuItemClickListener {
private MenuScene menuChildScene;
private final int MENU_PLAY = 0;
private final int MENU_SCORES = 1;
GameActivity act;
@Override
public void createScene() {
// TODO Auto-generated method stub
createBackground();
createMenuChildScene();
}
@Override
public void onBackKeyPressed() {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public SceneType getSceneType() {
// TODO Auto-generated method stub
//return the proper scene type
return SceneType.SCENE_MENU;
}
@Override
public void disposeScene() {
// TODO Auto-generated method stub
}
public void createBackground()
{
attachChild(new Sprite(0, 0, resourcesManager.menu_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 playMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_PLAY, resourcesManager.play_region, vbom), 1.2f, 1);
final IMenuItem scoresMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_SCORES, resourcesManager.scores_region, vbom), 1.2f, 1);
menuChildScene.addMenuItem(playMenuItem);
menuChildScene.addMenuItem(scoresMenuItem);
menuChildScene.buildAnimations();
menuChildScene.setBackgroundEnabled(false);
playMenuItem.setPosition(playMenuItem.getX(), playMenuItem.getY() + 10);
scoresMenuItem.setPosition(327, 380);
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_PLAY:
//SceneManager.getInstance().createLMenuScene();
return true;
case MENU_SCORES:
//SceneManager.getInstance().createScoresScene();
return true;
default:
return false;
}
}
}
Explanations: In above code first we define the MenuChildScene called menuChildScene and then defined and initialized int variabled MENU_PLAY and MENU_SCORES.
Returned the scene type in method getSceneType().
Created background for our menu scene in method createBackground() and used it inside createScene().
Created menu child scene in method createMenuChildScene() and used it inside createScene().
Run the project and see your game main menu.
i received the following error how do i fix it as there is no any solution on the net
ReplyDelete[2014-03-26 17:53:28 - FruitCollector] ------------------------------
[2014-03-26 17:53:28 - FruitCollector] Android Launch!
[2014-03-26 17:53:28 - FruitCollector] adb is running normally.
[2014-03-26 17:53:28 - FruitCollector] Performing com.gmail.abihumcreatives.fruitcollector.MainActivity activity launch
[2014-03-26 17:53:28 - FruitCollector] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-03-26 17:53:30 - FruitCollector] Application already deployed. No need to reinstall.
[2014-03-26 17:53:30 - AndEngine] Could not find AndEngine.apk!
[2014-03-26 17:53:30 - AndEnginePhysicsBox2DExtension] Could not find AndEnginePhysicsBox2DExtension.apk!
[2014-03-26 17:53:30 - FruitCollector] Starting activity com.gmail.abihumcreatives.fruitcollector.MainActivity on device 0123456789ABCDEF
[2014-03-26 17:53:30 - FruitCollector] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.gmail.abihumcreatives.fruitcollector/.MainActivity }
Go to project 'AndroidManifest.xml' file and replace MainActivity to GameActivity. I think this should solve the issue. If issue persist let me know.
ReplyDelete