//man.png
//background.png
/*
*http://blog.programfan.com/blog.asp?author=qq14923349
*程序之巢制作,移动窗口TiledLayr类例子
*图片存在rec目录下,记得在存之前把格式转换成png,具体方法是:
1,点击开始
2,附件
3,图画板
4,打开图片
5,另存为(可以存作PNG了,不要改后续名,否则运行不了)
6,其他工具,可以选用PS,或者ACDSEE等等改
*/
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.*;
public class Xman extends MIDlet
{
public void startApp(){
new Man1(Display.getDisplay(this)).start();
}
public void pauseApp(){}
public void destroyApp(boolean e){}
class Man1 extends GameCanvas implements Runnable{
private Display display;
private Sprite xman;
private TiledLayer background;
private int Xbackground,Ybackground;
public Man1(Display d){
super(true);
display=d;
display.setCurrent(this);
}
private void start(){
try{
xman=new Sprite(Image.createImage("/man.png"),25,20);
background=new TiledLayer(16,1,Image.createImage("/background.png"),44,88);
setcell();
}
catch(Exception e){
}
Thread t=new Thread(this);
t.start();
}
private void setcell(){
int map[]={1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2};
for(int i=0;i<map.length;i++)
{
int column=i%16;
int row=(i-column)/1;
background.setCell(column,row,map[i]);
}
Xbackground=2;
Ybackground=2;
xman.setPosition(getWidth()/2,100);
background.setPosition(0,40);
}
public void run(){
Graphics g=getGraphics();
while(true){
update();
draw(g);
try{
Thread.sleep(100);
}
catch(Exception e){}
}
}
private void update()
{
int keyState=getKeyStates();
if((keyState&LEFT_PRESSED)!=0)
{
background.move(-Xbackground,0);
xman.nextFrame();
}
else if((keyState&RIGHT_PRESSED)!=0)
{
background.move(Xbackground,0);
xman.nextFrame();
}
}
private void draw(Graphics g){
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
background.paint(g);
xman.paint(g);
flushGraphics();
}
}
}
评论