goodssprite.java

来自「J2ME编程的50个例子,适合掌上系统的编程」· Java 代码 · 共 44 行

JAVA
44
字号
package demo;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;


public class GoodsSprite extends Sprite{
	//定义一组表示物品类型的数值
	public static final int GODDS_GOLD 		= 0;	//金元宝
	public static final int GODDS_DIAMOND 	= 1;	//钻石
	public static final int GODDS_FRUIT 		= 2;	//水果
	public static final int GODDS_TYPE_NUM 	= 3;	//类型总数
	private int m_nType = GODDS_GOLD;
	public GoodsSprite( Image image, int frameWidth, int frameHeight){
		super(image, frameWidth, frameHeight);		
		defineReferencePixel(frameWidth / 2, frameHeight / 2);		
		setVisible( false ); 				//刚产生时,并不可见
	}
	//获得物品的类型
	public int getType(){
		return m_nType;
	}
	//设置物品的类型
	public void setType( int type ){
		m_nType = type;
		if( m_nType < 0 || m_nType >= GODDS_TYPE_NUM )
			m_nType = GODDS_GOLD;
		setFrame(m_nType);
	}
	//让物品开始掉落,参数x与y分别是物品起始点的位置
	public void StartDrop( int x, int y ){
		setRefPixelPosition( x, y );
		setVisible( true );
	}
	//逻辑操作,产生并管理物品下落的动画
	public void Logic( int srcHeight ){
		if( isVisible() == false )
			return;
		int y = getRefPixelY();
		y = y + 5;
		setRefPixelPosition( getRefPixelX(), y );
		if( y >= srcHeight )
			setVisible( false );
	}
}

⌨️ 快捷键说明

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