npc.java

来自「一个J2ME的RPG游戏」· Java 代码 · 共 76 行

JAVA
76
字号
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class Npc extends Sprite implements Runnable{

	String name; //人物姓名或道具名称

	String message1; //对话1

	String message2; //对话2

	int npcx; //人物或道具在地图上的X坐标

	int npcy; //人物或道具在地图上的Y坐标
	//	int isren;                         //是人物还是道具,1是人物,0是道具
	//	boolean addbool=false;            //判断是否添加特殊道具

	Image img;
	
	boolean pause = false;
	
	int step = 1;
	
	int directionLeft = 4 , directionRight = 8;
	int temp = 0 , counter = directionRight;

	public Npc(Image arg0, int width, int height, String myname,
			String mymessage1, int x, int y, String mymessage2, Image imagetemp) {
		super(arg0, width, height);
		name = myname;
		message1 = mymessage1;
		npcx = x;
		npcy = y;
		message2 = mymessage2;
		img = imagetemp;
		this.setPosition(npcx, npcy);
	}

	public Npc(Image img, int x, int y) {
		super(img, x, y);
	}

	public void run() {
		while(true){
			if(!pause){
				this.setFrame(counter + temp);
				temp ++;
				if(temp == 4) temp = 0;
				npcx += step;
				this.setPosition(npcx, npcy);

				if(npcx >= 220){
					npcx = 220;
					counter = directionLeft;
					temp = 0;
					step = -step;
				}
				
				if(npcx <= 120){
					npcx = 120;
					counter = directionRight;
					temp = 0;
					step = -step;
				}
				
			}
			try {
				Thread.sleep(150);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}		
	}

}

⌨️ 快捷键说明

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