Humble new Class - Button Menu

  ... the case you feel the need for a new feature or want to submit one.

Humble new Class - Button Menu

Postby CapitanNerd » Mon Jun 13, 2011 5:26 pm

I am new on Java
I am new on Android Develpment
I am new on AndEngine

Perhabs I'm reinventing gun powder for all of that...

Situation: Can't get menu buttons to highlight or toogle on touch
Solution: This little new class I've developed.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.###########
  2.  
  3. import org.anddev.andengine.entity.scene.menu.item.AnimatedSpriteMenuItem;
  4. import org.anddev.andengine.input.touch.TouchEvent;
  5. import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
  6.  
  7. public class SpriteButton extends AnimatedSpriteMenuItem {
  8.         private boolean pressed=false;
  9.         enum T {BUTTON,TOGGLE};
  10.         private T type = T.BUTTON;
  11.        
  12.         public SpriteButton(int pID, TiledTextureRegion pTiledTextureRegion) {
  13.                 super(pID, pTiledTextureRegion);
  14.         }
  15.  
  16.         public SpriteButton(int pID, TiledTextureRegion pTiledTextureRegion,T type) {
  17.                 super(pID, pTiledTextureRegion);
  18.                 this.setType(type);
  19.         }
  20.  
  21.         public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
  22.                 switch(this.type){
  23.                         case BUTTON:
  24.                                 if (!this.pressed){
  25.                                         this.pressed=true;
  26.                                         this.nextTile();
  27.                                 }
  28.                                
  29.                                 if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP)  {
  30.                                         this.pressed=false;
  31.                                         this.nextTile();
  32.                                 }
  33.                         break;
  34.                         case TOGGLE:
  35.                                 if (!this.pressed){
  36.                                         this.pressed=true;
  37.                                         this.nextTile();
  38.                                 }
  39.  
  40.                                 if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP)  {
  41.                                         this.pressed=false;
  42.                                 }
  43.                         break;
  44.                 }
  45.  
  46.                 return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
  47.         }
  48.  
  49.         public void setType(T type) {
  50.                 this.type = type;
  51.         }
  52.  
  53.         public T getType() {
  54.                 return type;
  55.         }
  56. }
  57.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


And here is an example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 this.setPauseBackGround(new Sprite(0,0,this.getPauseBackGroundTextureRegion()));
  2.                
  3.                 this.setPauseMenu(new MenuScene(this.getCamera()));
  4.                 final SpriteButton continueMenuItem  = new SpriteButton(10,this.getPauseContinueTextureRegion());
  5.                 final SpriteButton mainMenuMenuItem = new SpriteButton(11,this.getPauseMainMenuTextureRegion());
  6.                 final SpriteButton musicTurnMenuItem = new SpriteButton(12,this.getPauseMusicTurnTextureRegion(),T.TOGGLE);
  7.                
  8.                 this.getPauseMenu().addMenuItem(continueMenuItem);
  9.                 this.getPauseMenu().addMenuItem(mainMenuMenuItem);
  10.                 this.getPauseMenu().addMenuItem(musicTurnMenuItem);
  11.                
  12.                 this.getPauseMenu().buildAnimations();
  13.                 this.getPauseMenu().setBackgroundEnabled(false);
  14.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


The only "requirement" is to have a TiledTextureRegion as the image source with 2 tiles. The idea is that these menu items will highlight itselfs when touched using the second tile in case of the buttons and toggle between the 2 tiles in case of the toggles.
CapitanNerd
 
Posts: 21
Joined: Thu Jun 09, 2011 6:39 pm

Re: Humble new Class - Button Menu

Postby lopez1_de » Fri Jun 24, 2011 3:45 pm

I'm really wondering why this isn't already part of AndEngine. Or did I missed something?
lopez1_de
 
Posts: 69
Joined: Fri Jun 10, 2011 9:27 am

Re: Humble new Class - Button Menu

Postby kblood » Thu Jul 07, 2011 8:27 pm

I wonder if this would work with what I am testing?

I am using the BoundCamera, and ChaseCamera functions, and they do not seem to work well with the built in menu functions, shown in the android examples, because they all want to use "this.scene" something, and "this.BoundCamera". Which most my testing script does not support, and for that reason I am now considering just having a menu before beginning the game, and then using that common menu in the bottom of the screen that you can have come up when pressing menu. I think it will do for my first few test games.

But maybe this menu would work with my project? Or is this the menu that already is in the andengine? Doesnt look like it at first glance.
kblood
 
Posts: 333
Joined: Wed May 25, 2011 10:13 pm

Re: Humble new Class - Button Menu

Postby CapitanNerd » Fri Sep 16, 2011 9:39 pm

Sorry for the late answer.

No, this is a new class based on AnimatedSprite. It does not have anything in common with the menu system of AndEngine.
CapitanNerd
 
Posts: 21
Joined: Thu Jun 09, 2011 6:39 pm

Re: Humble new Class - Button Menu

Postby Zagahlo » Wed Feb 15, 2012 1:45 am

excellent!
Zagahlo
 
Posts: 25
Joined: Sun Nov 14, 2010 6:58 pm


Return to Features

Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests