📄 hero.java
字号:
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.TiledLayer;
public class Hero extends Sprite
{
private int x;
private int y;
private BraveCanvas braveCanvas;
private BraveManager braveManager;
public Hero(Image image, int frameWidth, int frameHeight)
{
super(image, frameWidth, frameHeight);
}
public void setBraveCanvas(BraveCanvas braveCanvas)
{
this.braveCanvas = braveCanvas;
}
public void setBraveManager(BraveManager braveManager)
{
this.braveManager = braveManager;
}
public void setManager(BraveCanvas braveCanvas)
{
this.braveCanvas = braveCanvas;
}
public void init(int x, int y)
{
this.x = x;
this.y = y;
}
public void afresh()
{
setPosition(this.x, this.y);
}
public void moveUp(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
if(!eventActionExist(99) && !eventActionExist(98))
this.y = Math.max(0, y - 1);
}
public void moveDown(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
if(!eventActionExist(99) && !eventActionExist(98))
this.y = Math.min(braveManager.getLayerAt(1).getHeight(), y + 1);
}
public void moveLeft(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
if(!eventActionExist(99) && !eventActionExist(98))
this.x = Math.max(0, x - 1);
}
public void moveRight(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
if(!eventActionExist(99) && !eventActionExist(98))
this.x = Math.min(braveManager.getLayerAt(1).getWidth(), x + 1);
}
//实现人物的对话
public void talk(String addressor,Image talkImage, String s, Graphics g)
{
g.drawImage(talkImage, 0, 0, Graphics.TOP|Graphics.LEFT);
g.drawString(addressor+":", 7, 6, Graphics.TOP|Graphics.LEFT);
for(int i = 0 ; i < s.length() ; i++)
{
g.drawString(s.substring(i, i+1), (i*12)+12, 21,
Graphics.TOP|Graphics.LEFT);
try
{
Thread.sleep(100);
}
catch(Exception e)
{
e.printStackTrace();
}
braveCanvas.flushGraphics();
}
//对话的文字形式结束,进入等待状态,直到
//再次按下对话键
while(BraveCanvas.isTalk)
{
try
{
int keystates = braveCanvas.getKeyStates();
//再次按下对话键
if(keystates == BraveCanvas.FIRE_PRESSED)
{
//是否对话标志位置为false
BraveCanvas.isTalk = false;
//是否可以重新开始对话标志位置为false
//之所以这样做是保证在在下一次检测按键时,不重新开始对话
BraveCanvas.isTalkSign = false;
break;
}
Thread.sleep(50);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public boolean eventActionExist(int eventID)
{
int xmax = (getX() + getWidth()) / 16;
int ymax = (getY() + getHeight()) / 16;
int xmin = getX() / 16;
int ymin = getY() / 16;
if(BraveCanvas.way == BraveCanvas.UP_PRESSED && getY()
% 16 == 0)
{
if((getX() + getWidth()) % 16 == 0)
xmax -= 1;
for(int i = xmin ; i <= xmax ; i++)
{
if(Scene.getEvent(i, ymin-1) == eventID)
return true;
}
}
else if(BraveCanvas.way == BraveCanvas.DOWN_PRESSED &&
(getY()+getHeight()) % 16
== 0)
{
if((getX() + getWidth()) % 16 == 0)
xmax -= 1;
for(int i = xmin ; i <= xmax ; i++)
{
if(Scene.getEvent(i, ymax) == eventID)
return true;
}
}
else if(BraveCanvas.way == BraveCanvas.LEFT_PRESSED &&
getX() % 16 == 0)
{
if((getY() + getHeight()) % 16 == 0)
ymax -= 1;
for(int i = ymin ; i <= ymax ; i++)
{
if(Scene.getEvent(xmin-1, i) == eventID)
return true;
}
}
else if(BraveCanvas.way == BraveCanvas.RIGHT_PRESSED &&
(getX()+getWidth()) % 16
== 0)
{
if((getY() + getHeight()) % 16 == 0)
ymax -= 1;
for(int i = ymin ; i <= ymax ; i++)
{
if(Scene.getEvent(xmax, i) == eventID)
return true;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -