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


package demo; 
import javax.microedition.lcdui.Image; 
import javax.microedition.lcdui.game.GameCanvas; 
import javax.microedition.lcdui.game.Sprite; 
 
 
public class BarSprite extends Sprite{ 
	private int m_nTime;				//取得武器后,停留的时间 
	public BarSprite( Image image, int frameWidth, int frameHeight){ 
		super(image, frameWidth, frameHeight);		 
		defineReferencePixel(frameWidth / 2, frameHeight / 2); 
	} 
	//设置木棒加长,参数blong为true,则设置成长型木棒,否则设置成普通木棒 
	public void setLong( boolean blong ){		 
		if( blong ){ 
			setFrame(1); 
			m_nTime = 300; 
		} 
		else 
			setFrame(0); 
	} 
	//根据玩家的输入,控制木棒的位置 
	public void Input(int keyStates, int scrWidth ){ 
		int x = getRefPixelX(); 
		if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 ) 
			x -= 5; 
		if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 ) 
			x += 5; 
		if( x < this.getWidth() / 2 ) 
			x = this.getWidth() / 2; 
		if( x > scrWidth - this.getWidth() / 2 ) 
			x = scrWidth - this.getWidth() / 2; 
		setRefPixelPosition(x, getRefPixelY()); 
	} 
	//逻辑操作,如果木棒处于加长状态,则计算加长状态所能保持的时间 
	public void Logic(){ 
		int n = getFrame(); 
		if( n == 1 ){ 
			m_nTime --; 
			if( m_nTime < 0 ) 
				setLong( false ); 
		} 
	} 
}