;
this.x = Math.min(braveCanvas.getWidth(), x + 1);
}
}
然后把BraveCanvas类稍做修改,把"@"换成英雄
BraveCanvas.java
package brave;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Graphics;
import java.io.IOException;
import javax.microedition.lcdui.Image;
public class BraveCanvas extends GameCanvas implements Runnable
{
private boolean sign;
private Graphics g;
private Hero hero;
public BraveCanvas()
{
super(true);
}
public void startup()
{
this.sign = true;
try
{
Image heroimage = Image.createImage("/hero_up.png");
hero = new Hero(heroimage, 17, 26);
hero.setBraveCanvas(this);
hero.init(40,40);
}
catch(Exception e)
{
e.printStackTrace();
}
Thread thread = new Thread(this);
thread.start();
}
public void run()
{
g = getGraphics();
while(sign)
{
try
{
input(g);
paint(g);
Thread.sleep(15);
}
catch(Exception e)
{
System.out.println("2:"+e);
}
}
}
public void input(Graphics g) throws IOException
{
int keystates = getKeyStates();
switch(keystates)
{
case UP_PRESSED:
hero.moveUp();
break;
case DOWN_PRESSED:
hero.moveDown();
break;
case LEFT_PRESSED:
hero.moveLeft();
break;
case RIGHT_PRESSED:
hero.moveRight();
break;
}
hero.afresh();
}
public void paint(Graphics g)
{
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffffff);
hero.paint(g);
flushGraphics();
}
}
这样,我们的英雄就可以在画面上移动了,不过英雄比较简单,没有hp,mp等等,只是一个图片。而且如果大
家感觉英雄的脚步过快,可以在BraveCanvas类中的Hero初始化时设置:
hero.setFrameSequence(new int[]{0, 0, 1, 1, 0, 0, 2, 2 });
暂时先写到这,在下次中我会把地图和碰撞检测加入。由于本人也是初学者,上面的代码有什么不妥的地方,请高手兄们多多赐教。msn:zhagy_1981@hotmail.com