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

📄 enemyfly.java

📁 基于J2ME 开发的冒险类手机游戏
💻 JAVA
字号:
package zonja;

import java.io.DataInputStream;
import java.io.DataOutputStream;


public class EnemyFly extends ZonSprite {

	public EnemyFly(int scrWidth, int scrHeight) {
		super(scrWidth, scrHeight);
		setRealSize(32, 24);
	}

	public static final int SPEED_X = 5;
	
	public static final int SPEED_Y = 5;
	
/*	public static final int SPEED_X = 7;
	
	public static final int SPEED_Y = 7;*/
	
	public static final int FROZEN_TIME = 40;
	
	public static final int WARNNING_TIME = 32;
	
	private boolean isCaptured = false;
	
	private boolean isKicked = false;
	
	private int captureTimer = 0;

	private int startX = 0;

	private int endX = 0;
	
	private int topY = 0;
	
	private int bottomY = 0;
	



//	private int frameTimeCount = 0;
	
	private int liftDirection = LIFTDIRECTION_DOWN;
	
	public boolean saveData(DataOutputStream dos){
		if(!super.saveData(dos))
			return false;
		try {
			dos.writeBoolean(isCaptured);
			dos.writeBoolean(isKicked);
			dos.writeByte(liftDirection);
			dos.writeInt(captureTimer);
		}
		catch(Exception e) {
			return false;
		}
		
		return true;
	}
	
	public boolean loadData(DataInputStream dis){
		if(!super.loadData(dis))
			return false;
		try {
			isCaptured = dis.readBoolean();
			isKicked = dis.readBoolean();
			liftDirection = dis.readByte();
			captureTimer = dis.readInt();
		}
		
		catch(Exception e) {
			return false;
		}
		
		return true;
	}

	void mainAction() {
		hVelocity = 0;
		vVelocity = 0;
		
		//be hitted by Lead
		if(isKicked){
			if(moveDirection == MOVEDIRECTION_RIGHT)
				move(4 * SPEED_X, -4 * SPEED_Y);
			else
				move(-4 * SPEED_X, -4 * SPEED_Y);
			return;
		}
		if(isCaptured){
			if(captureTimer++ > FROZEN_TIME){
				isCaptured = false;
				captureTimer = 0;
			}
			if(captureTimer > WARNNING_TIME){
				if(captureTimer % 4 == 0)
					isVisible = false;
				else
					isVisible = true;
			}
			
			if(moveDirection == MOVEDIRECTION_LEFT)
				frameIndex = 2;
			else 
				frameIndex = 5;
			return;
		}
		
		if(offsetX < scrWidth && offsetX + frameWidth > 0){
			isVisible = true;
		}else 
			isVisible = false;
		
		//main action
		if (moveDirection == MOVEDIRECTION_LEFT) {
			hVelocity = -EnemyFly.SPEED_X;
			if (frameIndex == 0)
				frameIndex = 1;
			else
				frameIndex = 0;
		} else {
			hVelocity = +EnemyFly.SPEED_X;
			if (frameIndex == 3)
				frameIndex = 4;
			else
				frameIndex = 3;
		}
		
		if(liftDirection == LIFTDIRECTION_UP){
			vVelocity = -EnemyFly.SPEED_Y;
		}else{
			vVelocity = EnemyFly.SPEED_Y;
		}
		
		if (worldOffsetX + hVelocity <= startX) {
			hVelocity = startX - worldOffsetX;
			moveDirection = MOVEDIRECTION_RIGHT;
		}else if (worldOffsetX + hVelocity >= endX) {
			hVelocity = endX - worldOffsetX;
			moveDirection = MOVEDIRECTION_LEFT;
		}
		
		if (worldOffsetY + vVelocity <= topY) {
			vVelocity = topY - worldOffsetY;
			liftDirection = LIFTDIRECTION_DOWN;
		}else if (worldOffsetY + vVelocity >= bottomY) {
			vVelocity = bottomY - worldOffsetY;
			liftDirection = LIFTDIRECTION_UP;
		}
		if(hVelocity == 0 && vVelocity == 0) 
			return;
		else{
			move(hVelocity, vVelocity);
			changeWorldOffset(hVelocity, vVelocity);			
		}
	}

	public void setStartOffset(int start, int end, int top, int bottom) {
		startX = start;
		endX = end;
		topY = top;
		bottomY = bottom;
	}
	
	public void setCaptured(boolean b){
		isCaptured = b;
	}
	
	public boolean getCaptured(){
		return isCaptured;
	}
	
	public void setKicked(boolean b){
		isKicked = b;
	}

	public boolean isKicked(){
		return isKicked;
	}

}

⌨️ 快捷键说明

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