帮我看下我的问题出在哪里?为什么会是这样的效果
Sprite.rar
(21 KB)
代码在这里
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Game_MIDlet5 extends MIDlet {
private MyGameCanvas myc;
Display dis;
public Game_MIDlet5() {
myc = new MyGameCanvas();
dis = Display.getDisplay(this);
dis.setCurrent(myc);
}
protected void startApp() throws MIDletStateChangeException {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
class MyGameCanvas extends GameCanvas implements Runnable {
boolean up = false, down = false, left = false, right = false;
Image image;
Graphics g;
public Sprite sprite;
int n = 0;
int z = 0;
int key;
public MyGameCanvas() {
super(false);
g = this.getGraphics();
try {
image = Image.createImage("/yidong.png");
sprite = new Sprite(image, 24, 29);
sprite.setFrame(6);
int nPosx = getWidth() / 2 - 25;
int nPosy = getHeight() / 2 - 25;
sprite.setPosition(nPosx, nPosy);
} catch (Exception e) {
e.printStackTrace();
}
image = null;
start();// 调用启动方法
}
public void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
sprite.paint(g);
int n = sprite.getFrame();
if (n >= 8) {
n = 0;
}
sprite.setFrame(n);
}
public void cycle() {
if (sprite.getX() > 0 && sprite.getX() < getWidth() - 16 && sprite.getY() > 0
&& sprite.getY() < getHeight() - 32) {
if (up) {
sprite.setPosition(sprite.getX(), sprite.getY() - 2);// 上
sprite.setFrameSequence(new int[] { 0, 1, 2 });
}
if (down) {
sprite.setPosition(sprite.getX(), sprite.getY() + 2);// 下
sprite.setFrameSequence(new int[] { 6, 7, 8 });
}
if (left) {
sprite.setPosition(sprite.getX() - 2, sprite.getY()); // 左
sprite.setFrameSequence(new int[] { 9, 10, 11 });
}
if (right) {
sprite.setPosition(sprite.getX() + 2, sprite.getY()); // 右
sprite.setFrameSequence(new int[] { 3, 4, 5 });
}
}
}
public void keyPressed(int keycode) {
switch (getGameAction(keycode)) {
case UP:
up = true;
break;
case DOWN:
down = true;
break;
case LEFT:
left = true;
break;
case RIGHT:
right = true;
break;
}
}
public void keyReleased(int keycode) {
switch (getGameAction(keycode)) {
case UP:
up = false;
break;
case DOWN:
down = false;
break;
case LEFT:
left = false;
break;
case RIGHT:
right = false;
break;
}
}
public void run() {
while (true) {
try {
System.out.println("55555555555");
repaint();
Thread.sleep(200);
cycle();
} catch (Exception ex) {
}
}
}
public void start() {
Thread t = new Thread(this);
t.start();
}
}
}