最近开发一款手机游戏 调用精灵的nextframe方法却只能循环到第二帧 请大家帮忙看下 谢啦
public class Game extends GameCanvas implements Runnable{
private Sprite s,p;
private LayerManager lm;
private Image imgbg;
private Image imgp;
private int px,py;
public Graphics offg;
public int[] FRAME_SEQUENCE_DOWN={0, 1, 2, 3};
public int[] FRAME_SEQUENCE_UP={12,13,14,15};
public int[] FRAME_SEQUENCE_LEFT={4, 5, 6, 7};
public int[] FRAME_SEQUENCE_RIGHT={8,9,10,11 };
public Game() {
super(true);
px=12;
py=23;
lm=new LayerManager();
try {
imgbg=Image.createImage("/bg.png");
imgp=Image.createImage("/frames.png");
} catch (IOException e) {}
s=new Sprite(imgbg);
p=new Sprite(imgp,24,36);
lm.append(p);
lm.append(s);
lm.setViewWindow(0, 0, getWidth(), getHeight());
offg=this.getGraphics();
(new Thread(this)).start();
}
public void run() {
while(true){
input();
offg.setColor(255);
offg.fillRect(0,0,getWidth(),getHeight());
lm.paint(offg,0, 0);
paint();
try {
Thread.currentThread();
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(){
this.flushGraphics();
}
public void input(){
int k=this.getKeyStates();
System.out.println("k="+k);
if((k&GameCanvas.DOWN_PRESSED)>0){
int[] FRAME_SEQUENCE_DOWN={0, 1, 2};
p.setFrameSequence(FRAME_SEQUENCE_DOWN);
p.nextFrame();
System.out.println(p.getFrame());
p.move(0, 4);
}
if((k&GameCanvas.UP_PRESSED)>0){
p.setFrameSequence(FRAME_SEQUENCE_UP);
p.nextFrame();
p.move(0, -4);
}
if((k&GameCanvas.LEFT_PRESSED)>0){
p.setFrameSequence(FRAME_SEQUENCE_LEFT);
p.nextFrame();
p.move(-4, 0);
}
if((k&GameCanvas.RIGHT_PRESSED)>0){
p.setFrameSequence(FRAME_SEQUENCE_RIGHT);
p.nextFrame();
p.move(4,0);
}
if((k&GameCanvas.FIRE_PRESSED)>0){
boolean vis=p.isVisible();
p.setVisible(!vis);
}
}
}
------解决方案--------------------
input 是一直在执行的 但是不会进入到if语句里面 J2ME有这样一个机制问题 就是你一直按键但是键值只是第一次才是你想要的键值 System.out.println(p.getFrame()); 应该只打印了一遍 要想实现效果的话 应该在keyPressed里面去定义k