程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME 小人移動實現

J2ME 小人移動實現

編輯:J2ME

import Javax.microedition.lcdui.*;
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();
           }
        }
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved