import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//import java.unit.*;
import java.io.*;
//简单的动画canvas by sword2008@程序之巢
public class test2 extends MIDlet{
private Display display;
public test2(){
}
public void startApp(){
new test(Display.getDisplay(this)).start();}
public void pauseApp(){ }
public void destroyApp(boolean unkowmn){ }
class test extends GameCanvas implements Runnable
{
//////////////////////////////////////
private Display display;
private long frameDelay;
private boolean sleeping;
// private Random rand;
private Sprite ken;
private int XSpeed,YSpeed;
private TiledLayer backgroundLayer;
//////////////////////////////////////
int layerMap[]={1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16};
//////////////////////////////////////
public test(Display d){
super(true);
display=d;
frameDelay=80; }
//==========================================
public void start(){
display.setCurrent(this);
XSpeed=YSpeed=0;
// rand=new Random();
try{ken= new Sprite(Image.createImage("/ken2.png"),64,58);
backgroundLayer=new TiledLayer(4,4,Image.createImage("/0.png"),32,32); }
catch(Exception e){System.out.println(e.toString());}
//------------------------------------------
sleeping=false;
//==============================
for(int i=0;i<layerMap.length;i++)
{int column=i%4;
int row=(i-column)/4;
backgroundLayer.setCell(column,row,layerMap[i]);
}
//==============================
Thread t=new Thread(this);
t.start();
}
//------------------------------------------
public void run(){
Graphics g=getGraphics();
while(!sleeping){
update();
draw(g);
try{Thread.sleep(frameDelay);}
catch(Exception e){}
}
}
private void draw(Graphics g){
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
//
backgroundLayer.paint(g);
ken.paint(g);
flushGraphics();
}
public void go(){
for(int i=0;i<3;i++)
{if(i==1)
ken.prevFrame();
else
ken.nextFrame();
}
}
public void update(){
//-------------------------------------------
int keyState=getKeyStates();
if((keyState&LEFT_PRESSED)!=0)
{XSpeed--;go();}
if((keyState&RIGHT_PRESSED)!=0)
{XSpeed++;go();}
//-------------------------------------------
ken.move(XSpeed,YSpeed);
/* for(int i=0;i<3;i++)
{if(i==1)
ken.prevFrame();
else
ken.nextFrame();
}*/
}
}
}
评论