浏览 1422 次
锁定老帖子 主题:J2ME 小人移动实现
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-12
import javax.microedition.midlet.*; public class movePNG extends MIDlet { private Display display; private moveActor moveactor; public movePNG() { display= Display.getDisplay(this); } public void startApp() { moveactor=new moveActor(); display.setCurrent(moveactor); } public void pauseApp() { } public void destroyApp(boolean flag) { } public void commandAction(Command command, Displayable displayable) { } class moveActor extends Canvas { Image actor; int walkActor; int goX; int goY; int moveX; goActor goactor; public moveActor() { try{ actor=Image.createImage("/actor.png"); }catch(Exception ex){} } public void keyPressed(int keyCode) { int gameKey = getGameAction(keyCode); switch (gameKey) { case UP: walkActor=1; break; case DOWN: walkActor=2; break; case LEFT: walkActor=3; break; case RIGHT: walkActor=4; break; } goactor=new goActor(); goactor.start(); goactor=null; } public void paint(Graphics g) { g.setColor(0xffffff); g.fillRect(0, 0, 180, 177); for(int j=0;j<4;j++) { g.translate(getWidth()/2-16+goX-moveX, getHeight()/2+goY+j*2-64); g.setClip(moveX, j*32, 32, 32); g.drawImage(actor, 0, 0, Graphics.LEFT | Graphics.TOP); g.translate(-getWidth()/2+16-goX+moveX, -getHeight()/2-goY-j*2+64); g.setClip(0, 0, 180, 177); } } class goActor extends Thread { public void run() { for(int i=0; i<3; i++) try{ if(walkActor==1) { goY=goY-10; moveX=5*32; } else if(walkActor==2){ goY=goY+10; moveX=0; } else if(walkActor==3){ goX=goX-10; if(i==2) moveX=0; else moveX=(i+1)*32; } else if(walkActor==4){ goX=goX+10; if(i==2) moveX=0; else moveX=(i+3)*32; } repaint(); Thread.sleep(500); }catch(Exception ex){} goX=0; goY=0; moveX=0; repaint(); } } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |