?? action.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 + -