⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 action.java

📁 移动平台游戏开发
💻 JAVA
字号:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

//动作按钮,及星形图像的显示管理类
public class Action {
	public static final int STATE_HIDE		= 0;		//隐藏
	public static final int STATE_SELECT 		= 1;		//玩家选择操作
	public static final int STATE_COMPUTER	= 2;		//电脑动作(吃牌等)
	public static final int STATE_STATE_NUM	= 3;
	private int m_nState = STATE_HIDE;
	
	public static final int ACTION_HU			= 0;		//胡
	public static final int ACTION_GANG		= 1;		//杠
	public static final int ACTION_PENG		= 2;		//碰
	public static final int ACTION_CHI		= 3;		//吃
	public static final int ACTION_PASS		= 4;		//不选则
	public static final int ACTION_TYPE_NUM	= 5;
	private boolean m_bAnGang;
	
	private Sprite m_StarSp;					//星形图像,显示电脑的动作
	private Sprite m_ButtonSp;					//按钮图像,玩家选择动作
	private boolean m_aButton[];				//按钮动作是否有效的标志(例如有时不允许吃牌)
	private int	m_nAction;					//当前玩家选择的动作
	private int m_nBnX;						//按钮起始显示位置的X坐标
	private int m_nBnY;						//按钮起始显示位置的Y坐标
	private Image	m_ArrowImg;					//箭头图像
	
	//构造方法,参数arrow为箭头图像,scrWidth、scrHeight分别是屏幕的宽和高
	public Action( Image arrow, int scrWidth, int scrHeight ){
		m_ArrowImg = arrow;
		try{
			//读取按钮图像
			Image img = Image.createImage("/demo/action.png");
			m_ButtonSp = new Sprite( img, 23, 14 );
			m_ButtonSp.defineReferencePixel(11, 12);
			m_aButton = new boolean[m_ButtonSp.getFrameSequenceLength()];
			m_aButton[ACTION_PASS] = true;
			m_nBnX = ( scrWidth - m_ButtonSp.getWidth()*5 ) / 2;
			m_nBnY = scrHeight - 40; 
			
			//读取星形图像
			img = null;
			img = Image.createImage("/start.png");
			m_StarSp = new Sprite( img, 26, 26 );
			m_StarSp.defineReferencePixel(13, 13);
			int x = scrWidth / 2;
			int y = 40;
			m_StarSp.setRefPixelPosition( x, y);
		}
		catch(Exception exception){}
	}
	//设置状态为隐藏
	public void setHide(){
		m_nState = STATE_HIDE;
	}
	//设置状态为选择按钮
	//四个参数分别是各种按钮是否有效的标志
	public void setSelect( boolean bHu, boolean bGang, 
								boolean bPeng, boolean bChi, boolean bAnGang){
		if( !bHu && !bGang && !bPeng && !bChi && !bAnGang )
			return;
		m_nState = STATE_SELECT;
		m_aButton[ACTION_HU] 	= bHu;
		m_aButton[ACTION_GANG] 	= bGang || bAnGang;
		m_aButton[ACTION_PENG] 	= bPeng;
		m_aButton[ACTION_CHI] 	= bChi;
		m_nAction = ACTION_HU;
		m_bAnGang = bAnGang;
	}
	//设置电脑动作
	public void setComputer( int action ){
		if( action < 0 || action >= ACTION_TYPE_NUM )
			return;
		m_StarSp.setFrame(action);
	}
	//处理按键操作
	//返回玩家选择的按钮类型,返回-1表示没有选择操作
	public int Input( int keyStates ){
		//如果不处于玩家选择按钮的状态,则直接退出
		if( m_nState != STATE_SELECT )
			return -1;
		if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 )
			m_nAction --;
		if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 )
			m_nAction ++;
		if( m_nAction < 0 )
			m_nAction = ACTION_TYPE_NUM - 1;
		if( m_nAction >= ACTION_TYPE_NUM )
			m_nAction = 0;
		
		if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 ){
			if( m_aButton[m_nAction] )
				return m_nAction;
		}
		return -1;
	}
	public boolean isAnGang(){
		return m_bAnGang;
	}
	//显示图像,参数g对应手机屏幕
	public void Paint(Graphics g){
		switch( m_nState ){
		case STATE_HIDE:
			break;
		case STATE_SELECT:
			int x = m_nBnX;
			int y = m_nBnY;
			int DisY = m_ButtonSp.getHeight() + m_ArrowImg.getHeight();
			for( int n = 0; n < ACTION_TYPE_NUM; n ++ )
			{
				if( m_aButton[n] )			//显示有效的按钮
					m_ButtonSp.setFrame(n);
				else						//显示无效的按钮
					m_ButtonSp.setFrame(n+ACTION_TYPE_NUM);
				m_ButtonSp.setRefPixelPosition(x, y);
				m_ButtonSp.paint(g);
				if( n == m_nAction )
				{//显示箭头
					g.drawImage(m_ArrowImg, x, y - DisY, 
							Graphics.HCENTER|Graphics.TOP);
				}
				x = x + m_ButtonSp.getWidth();
			}
			break;
		case STATE_COMPUTER:
			m_StarSp.paint(g);				//显示电脑的动作
			break;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -