⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carsprite.java

📁 一款赛车小游戏
💻 JAVA
字号:
package demo;
import java.util.Random;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;


public class CarSprite extends Sprite{
	protected int m_nSpeedX = 0;				//X方向运动的速度
	protected int m_nSpeedY = 8;				//Y方向运动的速度
	protected int m_nLastMovX;					//X方向的上次移动的距离
	protected int m_nLastMovY;					//X方向的上次移动的距离
	protected boolean m_bStop = false;		//停止的标志
	CarSprite(Image image, int frameWidth, int frameHeight) {
		super(image, frameWidth, frameHeight);
		defineReferencePixel( frameWidth / 2, frameHeight / 2 );
	}
	//向前移动,参数x、y分别是移动的X、Y距离
	public void MoveAhead(int x, int y){
		m_nLastMovX = x;
		m_nLastMovY = y;
		setRefPixelPosition(getRefPixelX()+x, getRefPixelY()+y);
	}
	//向后移动
	public void MoveBack( boolean bDecSpeed ){
		MoveAhead( -m_nLastMovX, -m_nLastMovY );
		if( bDecSpeed ){
			m_nSpeedY -= 2;
			if( m_nSpeedY < 0 )
				m_nSpeedY = 0;
		}
	}
	//逻辑操作,产生运动动画
	public void Logic(Random rand){
		if( m_bStop )
			return;
		if( rand != null ){
			m_nSpeedX = rand.nextInt() % 8;
		}
		//沿速度方向前移动
		MoveAhead(m_nSpeedX, -m_nSpeedY);
	}
	public void Stop(){
		m_bStop = true;
	}
}

⌨️ 快捷键说明

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