Question

I've got a problem with Andengine

I didn't find a class for checkboxes or switches (on/off) in Andengine so I want to create a button which I can press and it will stay pressed.

I tried with mButtonSprite.State.PRESSED but I don't know how to use it, maybe you can help me...

My code:

public class MainActivity extends SimpleBaseGameActivity{

    private int WIDTH = 1600;
    private int HEIGHT = 900;

    private BuildableBitmapTextureAtlas mBitmapTextureAtlas;

    private ITiledTextureRegion mButtonTextureRegion;

    public Sprite mButtonSprite;

    private State mState;


 @Override
 public EngineOptions onCreateEngineOptions() {
             final Camera mCamera = new Camera(0, 0, WIDTH, HEIGHT);

         return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,
             new RatioResolutionPolicy(WIDTH, HEIGHT),
             mCamera);
 }

 @Override
 protected void onCreateResources() {
                     BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(),WIDTH, HEIGHT,TextureOptions.BILINEAR);

      mButtonTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas,this,"rectangle_one.png", 2,1); 


      try {
             mBitmapTextureAtlas
                             .build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
                                             0, 2, 1));
             mBitmapTextureAtlas.load();
        } catch (TextureAtlasBuilderException e) {
             e.printStackTrace();
        }
 }

 @Override
 protected Scene onCreateScene() 
        {    
 this.mEngine.registerUpdateHandler(new FPSLogger(60));
 final Scene mScene = new Scene();

 mScene.getBackground().setColor(00000, 00000,00000);

 mScene.setTouchAreaBindingOnActionDownEnabled(true);


 ButtonSprite mButtonSprite = new ButtonSprite(900,450, mButtonTextureRegion,
            mEngine.getVertexBufferObjectManager()){

     @Override
     public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
             float pTouchAreaLocalX, float pTouchAreaLocalY){

         return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
     }

 };  

 mScene.registerTouchArea(mButtonSprite);

 mScene.attachChild(mButtonSprite);

 return mScene;
        }


}
Was it helpful?

Solution

Create sprite sheet of with two images say off and on (one row with 2 coloumns) .Then create an animates sprite like this

testsprite_music = new AnimatedSprite(0, 0,
                    resourcesManager.test_region_music, vbom) {
                @Override
                public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                        final float pTouchAreaLocalX, final float pTouchAreaLocalY) {


                    if (pSceneTouchEvent.isActionDown()) {

                        if (prefs.getBoolean("musiconoff", true)) {
                        //   Log.e("The music is on im going to off ","The music is on im going to off ");
                            android.content.SharedPreferences.Editor editor = prefs.edit();
                               editor.putBoolean( "musiconoff", false );
                               editor.commit(); 
                            testsprite_music.setCurrentTileIndex(0);
                        }
                        else
                        {// Log.e("The music is off im going to on ","The music is off im going to on ");
                            testsprite_music.setCurrentTileIndex(1);
                            android.content.SharedPreferences.Editor editor = prefs.edit();
                               editor.putBoolean( "musiconoff", true );
                               editor.commit(); 
                        }
                    }

                    return true;
                }
            };

OTHER TIPS

1. For CheckBox type:
if you want to enable and disable a button as check box use shared preference
For Example


    boolean isChecked = getFromPreference();



 if(isButtonEnbled)
      {
                  //   show checkSprite
       }else{
                  //   show unchecked Sprite
       }

2.To show a button when pressed

in onTouchArea we have three options



      if(ActionBegin){
           // fadeIn
        }else if(ActionEnd){
        //  fade out
        }

In this way we have many alternatives 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top