andengine Sprite按钮应保持按下状态

时间:2013-11-18 16:54:28

标签: android eclipse andengine pressed

我遇到了Andengine的问题

我没有找到Andengine中的复选框或开关(开/关)的类,所以我想创建一个按钮,我可以按下它并保持按下状态。

我试过mButtonSprite.State.PRESSED,但我不知道如何使用它,也许你可以帮助我......

我的代码:

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;
        }


}

2 个答案:

答案 0 :(得分:0)

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 

答案 1 :(得分:0)

使用两个图像创建精灵表,然后关闭和打开(一行有2列)。然后创建一个像这样的动画精灵

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;
                }
            };