www.pudn.com > 50code.rar > BKQSprite.java, change:2007-02-26,size:5628b


package demo; 
import javax.microedition.lcdui.Graphics; 
import javax.microedition.lcdui.Image; 
import javax.microedition.lcdui.game.Sprite; 
import javax.microedition.rms.RecordStore; 
 
 
public class BKQSprite extends Sprite{ 
	//精灵状态值 
	public static final int BKQ_WALK 		= 0;		//走路 
	public static final int BKQ_TALK		= 1;		//谈话 
	public static final int BKQ_SLEEP 	= 2;		//睡觉 
	public static final int BKQ_MUSIC		= 3;		//听音乐 
	public static final int BKQ_NORMAL 	= 4;		//正常 
	public static final int BKQ_DEAD 		= 5;		//死了 
	public static final int BKQ_STATE_NUM	= 6; 
	private int m_nState = BKQ_NORMAL; 
	private int m_nJoy = 50;							//兴奋度 
	private int m_nKind = 50;							//亲和度 
	private int m_nHealth = 50;						//健康度 
	//上面三个参数每隔m_nTime时间会自动减少 
	private int m_nTime = 600;							 
	private int m_nActionTime = 50;					//动作维持的时间 
	private Sprite m_Effect;							//特殊效果,如音符等 
	public BKQSprite( Image image, int frameWidth, int frameHeight){ 
		super(image, frameWidth, frameHeight);		 
		defineReferencePixel(frameWidth / 2, frameHeight / 2); 
		try{ 
			Image img = Image.createImage("/demo/effect.png"); 
			m_Effect = new Sprite(img, 15, 14); 
			m_Effect.setVisible(false); 
		} 
		catch (Exception ex){}  
		Load(); 
	} 
	//设置精灵位置 
	public void setAllPosition( int x, int y ){ 
		setPosition(x,y); 
		m_Effect.setPosition(x + 55,y + 30); 
	} 
	//获取当前状态 
	public int getState(){ 
		return m_nState; 
	} 
	//设置当前状态,每设置一次,会增加相应的参数值 
	public void setState(int state){ 
		m_nState = state; 
		switch( m_nState ){ 
		case BKQ_WALK: 
			m_nJoy -= 3; 
			m_nHealth += 5;	 
			m_Effect.setVisible(false); 
			m_nActionTime = 20; 
			break; 
		case BKQ_TALK: 
			m_nKind += 5; 
			m_Effect.setVisible(false); 
			m_nActionTime = 20; 
			break; 
		case BKQ_SLEEP: 
			m_nJoy += 4; 
			m_nHealth -=3; 
			m_nKind -= 6; 
			m_Effect.setVisible(true); 
			m_nActionTime = 20; 
			break; 
		case BKQ_MUSIC: 
			m_nJoy += 2; 
			m_nHealth += 3; 
			m_Effect.setVisible(true); 
			m_nActionTime = 20; 
			break; 
		case BKQ_NORMAL: 
		case BKQ_DEAD: 
			m_Effect.setVisible(false); 
			break; 
		}	 
		Save(); 
	} 
	//逻辑操作,产生精灵动画,并判断精灵是否死亡 
	public void Logic(){ 
		int n = getFrame(); 
		switch( m_nState ){ 
		case BKQ_WALK: 
			if( n == 3 ) 
				setFrame(4); 
			else 
				setFrame(3); 
			break; 
		case BKQ_TALK: 
			if( n == 1 ) 
				setFrame(2); 
			else 
				setFrame(1); 
			break; 
		case BKQ_SLEEP: 
			setFrame(0); 
			n = m_Effect.getFrame(); 
			if( n == 2 ) 
				m_Effect.setFrame(3); 
			else 
				m_Effect.setFrame(2); 
			break; 
		case BKQ_MUSIC: 
			setFrame(2); 
			n = m_Effect.getFrame(); 
			if( n == 0 ) 
				m_Effect.setFrame(1); 
			else 
				m_Effect.setFrame(0); 
			break; 
		case BKQ_NORMAL: 
			n ++; 
			if( n >= getFrameSequenceLength() ) 
				n = 0; 
			setFrame(n); 
			break; 
		case BKQ_DEAD: 
			setFrame(2); 
			return; 
		} 
		if( m_nJoy < 0 || m_nKind < 0 || m_nHealth < 0 ) 
			setState(BKQ_DEAD); 
		m_nTime --; 
		if( m_nTime < 0 ){ 
			m_nTime = 600; 
			m_nJoy -= 10; 
			m_nKind -= 10; 
			m_nHealth -= 10; 
		} 
		if( m_nActionTime > 0 ){ 
			m_nActionTime --; 
			if( m_nActionTime <= 0 ) 
				setState(BKQ_NORMAL); 
		} 
	} 
	//显示精灵,特殊效果,及文字信息 
	public void PaintAll(Graphics g){ 
		paint(g); 
		m_Effect.paint(g); 
		StringBuffer buffer = new StringBuffer(); 
		buffer.append("兴奋度:"); 
		buffer.append(m_nJoy); 
		buffer.append("  亲和度:"); 
		buffer.append(m_nKind); 
		buffer.append("  健康度:"); 
		buffer.append(m_nHealth); 
		g.drawString(buffer.toString(), 10, 10, 0 ); 
	} 
	//保存精灵的参数 
	public void Save(){ 
		try{			 
			byte b[] = new byte[4*3]; 
			int temp = m_nJoy; 
			for(int i = 11; i >= 8; i--){ 
				b[i] = new Integer(temp & 0xff).byteValue(); 
				temp = temp >> 8; 
			}		 
			temp = m_nKind; 
			for(int i = 7; i >= 4; i--){ 
				b[i] = new Integer(temp & 0xff).byteValue(); 
				temp = temp >> 8; 
			}		 
			temp = m_nHealth; 
			for(int i = 3; i >= 0; i--){ 
				b[i] = new Integer(temp & 0xff).byteValue(); 
				temp = temp >> 8; 
			}		 
			//打开RecordStore,没有则新建			 
			RecordStore rs = RecordStore.openRecordStore( 
					"BKQparam",true);			 
			if( rs.getNumRecords() > 0 ){	//如果记录数大于0 
				rs.setRecord(1, b, 0, b.length);	//更改第一条记录 
			}			 
			else{      
				//如果没有记录,则添加记录	 
				rs.addRecord(b,0,b.length);	 
			}		 
			rs.closeRecordStore(); 
		}	 
		catch (Exception ex){}	 
	}	 
	//读取精灵的参数 
	public void Load(){ 
		try{			 
			RecordStore rs =  
				RecordStore.openRecordStore( "BKQparam", false);			 
			byte b[] = rs.getRecord(1); 
			m_nHealth = 0; 
			for (int i = 0; i < 3; i++){ 
				//如果b[i]小于0,则将b[i]转换成相应的正数 
				if( b[i] < 0 )	 
					m_nHealth += ( b[i] + 256 ); 
				else 
					m_nHealth += b[i];	 
				m_nHealth *= 256;	 
			}					 
			m_nHealth += b[3];	 
			if( b[3] < 0 )	 
				m_nHealth += 256;	 
			 
			m_nKind = 0; 
			for (int i = 4; i < 7; i++){ 
				//如果b[i]小于0,则将b[i]转换成相应的正数 
				if( b[i] < 0 )	 
					m_nKind += ( b[i] + 256 ); 
				else 
					m_nKind += b[i];	 
				m_nKind *= 256;	 
			}					 
			m_nKind += b[7]; 
			if( b[7] < 0 )	 
				m_nHealth += 256;	 
			 
			m_nJoy = 0; 
			for (int i = 8; i < 11; i++){ 
				//如果b[i]小于0,则将b[i]转换成相应的正数 
				if( b[i] < 0 )	 
					m_nJoy += ( b[i] + 256 ); 
				else 
					m_nJoy += b[i];	 
				m_nJoy *= 256;	 
			}					 
			m_nJoy += b[11];	 
			if( b[11] < 0 )	 
				m_nJoy += 256;		 
			rs.closeRecordStore(); 
		} 
		catch (Exception ex){}	 
	} 
}