AndEngine Button(2)——点击切换显示图片Button

 
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.entity.layer.ILayer;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.detector.ClickDetector;
import org.anddev.andengine.input.touch.detector.ClickDetector.IClickDetectorListener;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;


import android.util.Log;


/**
 * @author Strong Chi
 * 2011-9-11 下午4:19:23
 * 点击按钮时图片会变,图片需要有两帧,来回切换
 */
public class XDStateButtonBase extends HUD implements IClickDetectorListener {

	
	//fields
	private AnimatedSprite mControlButton;	
	private final XDButtonClickListener mXDButtonClickListener;
	private boolean mStatus = false;
	
	//Constructors
	public XDStateButtonBase(final int pX, final int pY, final Camera pCamera, final TiledTextureRegion pOnRegion, final XDButtonClickListener pXDButtonClickListener) {
		this.setCamera(pCamera);
		this.mXDButtonClickListener = pXDButtonClickListener;  //控制外面
		this.mControlButton = new AnimatedSprite(pX, pY, pOnRegion) {
			//控制内部变化
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				//Log.i("ST", "onAreaTouched");
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
					Log.i("ST","XDStateButtonBase onclick");
					if(mStatus){
						this.setCurrentTileIndex(0);
						mStatus = false;
						if(mXDButtonClickListener!=null)	mXDButtonClickListener.onClick(this);
					}else{
						this.setCurrentTileIndex(1);
						mStatus = true;
					}
				}
				return true;
			}
		};
		this.mControlButton.setCurrentTileIndex(0);
		this.registerTouchArea(this.mControlButton);
		final ILayer bottomLayer = this.getBottomLayer();
		bottomLayer.addEntity(this.mControlButton);
		this.setTouchAreaBindingEnabled(true);
		
	}
	
	@Override
	public void onClick(ClickDetector pClickDetector, TouchEvent pTouchEvent) {
		//不知道如何触发
		Log.i("ST","onClick ST");
		
	}
	
	public interface XDButtonClickListener{
		public void onClick(final AnimatedSprite animatedSprite);
	}
}

你可能感兴趣的:(null,Class,import,float,button,interface)