📄 bulletsprite.java
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
public class BulletSprite extends Sprite {
//sprite resources
private Sprite m_babyBulletSprite;
//gun pop image
private Image m_bulletPopImage;//枪花资源
private Sprite m_bulletPopSprite;
private boolean m_bDisplayGunScene;
private int m_gunSceneFrames;
//constant definitions
private int m_startPosX = 0;//子弹由坦克射出的时候的初始坐标
private int m_startPosY = 0;
private int m_iCurrentBulletDirection = 0;
private int m_bulletOwer;
private int m_currentState;
private int m_bulletType;
private int m_bulletSpeed ;
private int m_bulletDamage;
private boolean m_threadAlive = true;//run 函数是否运行
private boolean m_bRunning = false;
private boolean m_init ;
private int m_tankIndex;
//constant macro
public final static int ANCHOR_LEFTTOP=1;
public final static int ANCHOR_CENTER=2;
private final static int BULLET_GUN_FRAMES=1;
//baby tank,bullet directions
public final static int BABY_BULLET_UP=0;
public final static int BABY_BULLET_RIGHT=1;
public final static int BABY_BULLET_DOWN=2;
public final static int BABY_BULLET_LEFT=3;
private final static int BULLETPOP_IMAGE_WIDTH = 12;
private final static int BULLETPOP_IMAGE_HEIGHT = 12;
//width height
public int getTankIndex(){
return m_tankIndex;
}
public BulletSprite(Image image, int frameWidth, int frameHeight,int speed,
int damage,int bulletOwner,int tankIndex,int bulletType) {
super(image, frameWidth, frameHeight);
defineReferencePixel(frameWidth / 2, frameHeight / 2);
m_currentState = -1;
m_bulletSpeed = speed;
m_bulletOwer = bulletOwner;
m_bulletDamage = damage;
m_bulletType = bulletType;
m_currentState = GameLogic.GAME_STATE_READY;
m_iCurrentBulletDirection = 0;
m_init = false; //load resource or not
m_tankIndex = tankIndex;
createGunpopScene();
}
public void pause(){
m_currentState = GameLogic.GAME_STATE_PAUSE;
m_threadAlive=true;
}
public void stop(){
m_currentState = GameLogic.GAME_STATE_END;
m_threadAlive = false;
setVisible(false);
releaseGunpopScene();
}
/**
* 检测一个特定点是否进入一个Actor内
* @param px 特定点的x坐标.
* @param py 特定点的y坐标
* @return 如果特定点进入Actor范围内,那么返回true,否则为false;
* ref point is located in the middle of the rectangele
*/
public boolean isCollidingWith(int px, int py){
try {
int xPos=getRefPixelX();
int yPos=getRefPixelY();
//get the location coordinate in (left,top)corner
//judge whether the point is inside the sprite.
if(px>=xPos-getWidth()/2&&px<=xPos+getWidth()/2&&py>=yPos-getHeight()/2&&py<=yPos+getHeight()/2){
return true;
}
return false;
}
catch (Exception ex) {
ex.printStackTrace();
}
return true;
}
/**
* 检测一个Actor对象是否碰上了当前的Actor对象。
* @param another 另一个Actor对象
* @return 如果another和当前对象发生碰撞,则返回true,否则为false.
*/
public boolean collidesWith(Sprite s,int refPoint){
try {
int leftTopX=0,leftTopY=0,rightTopX=0,rightTopY=0,leftBottomX=0,leftBottomY=0,rightBottomX=0,rightBottomY=0;
//judge the four points of the rectangle
if(refPoint==ANCHOR_CENTER){
leftTopX=s.getRefPixelX()-s.getWidth()/2;
leftTopY=s.getRefPixelY()+s.getHeight()/2;
rightTopX=s.getRefPixelX()+s.getWidth()/2;
leftBottomY=s.getRefPixelY()-s.getHeight()/2;
}else if(refPoint==ANCHOR_LEFTTOP){
leftTopX=s.getRefPixelX();
leftTopY=s.getRefPixelY();
rightTopX=s.getRefPixelX()+getWidth();
leftBottomY=s.getRefPixelY()+getHeight();
}
rightTopY=leftTopY;
leftBottomX=leftTopX;
rightBottomX=rightTopX;
rightBottomY=leftBottomY;
if(isCollidingWith(leftTopX,leftTopY)||
isCollidingWith(rightTopX,rightTopY)||
isCollidingWith(leftBottomX,leftBottomY)||
isCollidingWith(rightBottomX,rightBottomY)){
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
public int getGameState(){
return m_currentState;
}
public void start(){
m_bRunning = false;
m_threadAlive = true;
m_currentState = GameLogic.GAME_STATE_LOOP;
setVisible(true);
}
public void setRefPixelPosition(int x,
int y){
if (!m_bRunning) {
m_startPosX = x;
m_startPosY = y;
}
super.setRefPixelPosition(x, y);
}
public void setDirection(int angel){
m_iCurrentBulletDirection = angel;
}
public void changeBulletDamage(int damage){
m_bulletDamage=damage;
}
/**
*
* 1. 根据相应角度进行移动.(目前只考虑前后左右90度的情况)。
if(bullet is alive)
move fixed pixels in direction;
2.碰到障碍物的处理。
if(子弹碰到障碍物 or
子弹碰到地图边界 or
坦克被击毁 or
子弹击中敌方或者本方坦克)
子弹消失;
if(击中敌方坦克)
爆炸效果.
敌方生命值减小。
*/
public void run(){
m_bRunning = true;
if(!m_threadAlive){
return;
}
switch (m_currentState) {
case GameLogic.GAME_STATE_READY:
if(!m_init){
}
break;
case GameLogic.GAME_STATE_END:
reset();
break;
case GameLogic.GAME_STATE_LOOP:
if (Sprite.TRANS_NONE == m_iCurrentBulletDirection) {
m_startPosY -= m_bulletSpeed;
}
if (Sprite.TRANS_ROT180 == m_iCurrentBulletDirection) {
m_startPosY += m_bulletSpeed;
}
if (Sprite.TRANS_ROT90 == m_iCurrentBulletDirection) {
m_startPosX += m_bulletSpeed;
}
if (Sprite.TRANS_ROT270 == m_iCurrentBulletDirection) {
m_startPosX -= m_bulletSpeed;
}
setRefPixelPosition(m_startPosX, m_startPosY);
setTransform(m_iCurrentBulletDirection); //调整子弹头方向。
displayBabyBullet();
GameLogic.collidesWith(this, m_bulletOwer,
m_bulletDamage);
break;
case GameLogic.GAME_STATE_PAUSE:
if (m_bDisplayGunScene == true) {
displayGunPopScene();
}
break;
default:
break;
}
}
private void releaseGunpopScene() {
try {
m_bulletPopImage = null;
if (null != m_bulletPopSprite) {
GameLogic.m_layerManager.remove(m_bulletPopSprite);
m_bulletPopSprite = null;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void createGunpopScene() {
try {
m_bulletPopImage=null;
m_bulletPopImage = Image.createImage("/tank/bullet_res/gunpop.png");
m_bulletPopSprite = new Sprite(m_bulletPopImage, BULLETPOP_IMAGE_WIDTH,
BULLETPOP_IMAGE_HEIGHT);
m_bulletPopSprite.defineReferencePixel(BULLETPOP_IMAGE_WIDTH / 2,
BULLETPOP_IMAGE_HEIGHT / 2);
m_bulletPopSprite.setVisible(false);
GameLogic.m_layerManager.insert(m_bulletPopSprite, 0);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void startGupPopScene() {
m_bDisplayGunScene = true;
pause();
}
private void displayGunPopScene() {
try {
if (false == m_bulletPopSprite.isVisible()) {
m_bulletPopSprite.setVisible(true);
}
m_bulletPopSprite.setRefPixelPosition(getRefPixelX(), getRefPixelY());
m_gunSceneFrames++;
if (m_gunSceneFrames <= BULLET_GUN_FRAMES) {
m_bulletPopSprite.nextFrame();
}
else {
reset();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void displayBabyBullet() {
try {
if (m_bulletType != GameLogic.BULLET_TYPE_BABY) {
return;
}
/**
* 1.display one frame per circle;
* 2.set it invisible the time it collides with other things;
*/
nextFrame();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void reset(){
m_bRunning = false;
m_threadAlive = true;
m_startPosX = m_startPosY = 0;
m_iCurrentBulletDirection = 0;
m_currentState = GameLogic.GAME_STATE_READY;
//gun pop scenes
m_gunSceneFrames=0;
m_bDisplayGunScene=false;
try {
//bullet消失
setVisible(false);
if(null!=m_bulletPopSprite){
m_bulletPopSprite.setVisible(false);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -