www.pudn.com > ch8-spaceshooter.rar > Sprite.java


/* 
 * Sprite.java 
 * 
 * Copyright 2001 SkyArts. All Rights Reserved. 
 */ 
import javax.microedition.lcdui.*; 
 
/** 
 * Sprite类(显示贴图零件) 
 * 
 * @author  Hideki Yonekawa 
 * @version 1.0 
 */ 
abstract class Sprite { 
	/** 储存了X坐标的变量 */ 
	protected int				x; 
	/** 储存了Y坐标的变量 */ 
	protected int				y; 
	/** 储存横宽的变量 */ 
	protected int				width; 
	/** 储存高度的变量 */ 
	protected int				height; 
 
	/** 代表是否为Alive状态的标志变量 */ 
	protected boolean			isAlive				= true; 
	/** 代表是否为Hit状态的标志变量 */ 
	protected boolean			isHit				= false; 
	/** 储存了TicK计数的变量 */ 
	protected int				tickCount			= 0; 
 
	/** 
	 * 设定X坐标的方法 
	 * @param	x 要设定的X坐标值 
	 */ 
	void setX(int x) { 
		this.x = x; 
	} 
 
	/** 
	 * 传回X坐标的方法 
	 * @return	int	被设定的X坐标值 
	 */ 
	int getX() { 
		return x; 
	} 
 
	/** 
	 * 设定Y坐标的方法 
	 * @param	y	要设定的y坐标值 
	 */ 
	void setY(int y) { 
		this.y = y; 
	} 
 
	/** 
	 * 传回Y坐标值的方法 
	 * @return	int	被设定的Y坐标值 
	 */ 
	int getY() { 
		return y; 
	} 
 
	/** 
	 * 传回横宽的方法 
	 * @return	int	横宽值 
	 */ 
	int getWidth() { 
		return width; 
	} 
 
	/** 
	 * 传回高度的方法 
	 * @return	int	高度值 
	 */ 
	int getHeight() { 
		return height; 
	} 
 
	/** 
	 * 设定Alive状态的方法 
	 * @param isAlive	Alive状态的话就是true、不是Alive状态就是false 
	 */ 
	void setAlive(boolean isAlive) { 
		this.isAlive = isAlive; 
	} 
 
	/** 
	 * 传回Alive状态的方法 
	 * @return boolean	Alive状态就是true、不是Alive状态就是false 
	 */ 
	boolean isAlive() { 
		return isAlive; 
	} 
 
	/** 
	 * 设定Hit状态的方法 
	 * @param isAlive	Hit状态就是true、不是Hit状态就是false 
	 */ 
	void setHit(boolean isHit) { 
		this.isHit = isHit; 
		tickCount	 = 0; 
	} 
 
	/** 
	 * 传回Hit状态的方法 
	 * @return boolean	Hit状态就是true、不是Hit状态就是false 
	 */ 
	boolean isHit() { 
		return isHit; 
	} 
 
	/** 
	 * 传回这个Sprite(this)是否有与其他Sprite重叠的方法 
	 * @return boolean	有重叠的话就是true、没有重叠的话是false 
	 */ 
	boolean isOverlaps(Sprite otherSprite) { 
		if(	(otherSprite.getX() <= x && otherSprite.getX() + otherSprite.getWidth() >= x) ||	//右 
			(otherSprite.getX() >= x && otherSprite.getX() <= x + width) ) {			//左 
			if(	(otherSprite.getY() <= y && otherSprite.getY() + otherSprite.getHeight() >= y) ||//上  
				(otherSprite.getY() >= y && otherSprite.getY() <= y + height) ) {		//下 
				return true; 
			} 
		} 
		return false; 
	} 
 
	/** 要移动Sprite所调用的方法 */ 
	abstract void doMove(); 
 
	/** 要描绘Sprite所调用的方法 */ 
	abstract void doDraw(Graphics g); 
}