tanksprite.java

来自「Java ME手机应用开发大全一书的配套光盘上的源码」· Java 代码 · 共 83 行

JAVA
83
字号
import java.io.IOException;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
//定义代表坦克的精灵类
public class TankSprite extends Sprite {
	protected int m_nDir;
	//最后移动到的位置的X和Y坐标
	protected int m_nLastMovX	= 0;
	protected int m_nLastMovY	= 0;
	//移动的速度
	protected int m_nSpeed		= 0;
	public BulletSprite m_Bullet;  
	//构造函数
	public TankSprite(Image image, int frameWidth, int frameHeight) {
		super(image, frameWidth, frameHeight);
		defineReferencePixel(frameWidth / 2, frameHeight / 2);
		setVisible( false );
		try{
			//创建子弹类
			Image img = Image.createImage("/bullets.png");
			m_Bullet = new BulletSprite(img,3,3);
		}
		catch(IOException ioe){}
		catch(Exception e){}
	}
//	设置并显示代表坦克的精灵对象的方法
	public void Start( int nX, int nY, int nDir, int nSpeed ){
		SetDir( nDir );
		setRefPixelPosition( nX, nY );
		m_nSpeed = nSpeed;
		setVisible( true );
	}
//	不显示代表坦克的精灵对象的方法
	public void Stop() {
		setVisible(false);
	}
	
	public void Logic(){
		m_Bullet.Logic();
	}
	//设置坦克的方向
	public void SetDir( int nDir ){
		m_nDir = nDir;
		switch( m_nDir ){
		case 0:
			setTransform(TRANS_NONE);
			break;
		case 1:
			setTransform(TRANS_ROT90);
			break;
		case 2:
			setTransform(TRANS_ROT180);
			break;
		default:
			setTransform(TRANS_MIRROR_ROT270);
			break;
		}
	}
	//坦克移动的方法
	public void Move(int nX, int nY) {
		int nToX = getRefPixelX() + nX;
		int nToY = getRefPixelY() + nY;
		m_nLastMovX = nX;
		m_nLastMovY = nY;
		setRefPixelPosition( nToX, nToY );
	}
	//坦克后退的方法
	public void MoveBack() {
		Move( -m_nLastMovX, -m_nLastMovY );
		m_nLastMovX = 0;
		m_nLastMovY = 0;
	}
	//创建子弹
	public void CreateBullet(){
	    if( m_Bullet.isVisible() )
	    	return;
	    m_Bullet.setVisible(true);
	    int nX = getRefPixelX();
	    int nY = getRefPixelY();
	    m_Bullet.Start( nX, nY, m_nDir, 3 );
	}
}

⌨️ 快捷键说明

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