TiledLlayer:贴砖层,即用一块快的小图,完成一幅大背景图片的绘制,利用TiledLayer的move方法,实现滚动大背景;
try {
TiledLayer bgLayer=new TiledLayer(20,15,Image.createImage("/com/sk/imgs/bg0.png"), 32, 32);
} catch (IOException e) {
System.out.println("loading image error");
}
//newTiledLayer(int cols,int rows,Image,Widthpixel,Heightpixel); //列数,行数,图片,每砖宽包括几像素,每砖高几像素,这里是 20列,15行,砖宽32*高32,组合起来便是640*480的图片
bgLayer.setPosition(this.getWidth()-bgLayer.getWidth(), this.getHeight()-bgLayer.getHeight());
//设置 TiledLayer的初始位置
以下给出完整代码
1.com.sk.model 包
package com.sk.model;
import java.io.IOException;
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.lcdui.game.TiledLayer;
public class Wcanvas extends GameCanvas implements Runnable{
private Display display;
private Sprite person;
private TiledLayer bgLayer;
private int inputDelay;
private boolean sleeping;
private int frameDelay=33;
public Wcanvas(Display display) {
super(true);
// TODO Auto-generated constructor stub
this.display=display;
}
public void start(){
try {
bgLayer=new TiledLayer(20,15,Image.createImage("/com/sk/imgs/bg0.png"), 32, 32);
person=new Sprite(Image.createImage("/com/sk/imgs/fighter01.png"),32,48);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("loading image error");
}
bgLayer.setPosition(this.getWidth()-bgLayer.getWidth(), this.getHeight()-bgLayer.getHeight());
person.setPosition((this.getWidth()-person.getWidth())/2, (this.getHeight()-person.getHeight())/2);
display.setCurrent(this);
//
int x=0;
for(int i=0;i<15;i++){ //行
for(int j=0;j<20;j++){ //列
x++;
bgLayer.setCell(j, i, x);
}
}
//
new Thread(this).start();
}
public void stop(){
sleeping=true;
}
public void update(){
if(++inputDelay>2){
int x=0,y=0;
int keyState=this.getKeyStates();
if((keyState & this.LEFT_PRESSED)>0){
bgLayer.move(12, 0);
x--;
//左方向
if(person.getFrame()==5){
person.setFrame(7);
}else if(person.getFrame()==7){
person.setFrame(5);
}else {
person.setFrame(5);
}
}else if((keyState & this.RIGHT_PRESSED)>0){
bgLayer.move(-12, 0);
x++;
//右方向
if(person.getFrame()==9){
person.setFrame(11);
}else if(person.getFrame()==11){
person.setFrame(9);
}else{
person.setFrame(9);
}
}
if((keyState & this.UP_PRESSED)>0){
bgLayer.move(0, 12);
y--;
//上
if(person.getFrame()==13){
person.setFrame(15);
}else if(person.getFrame()==15){
person.setFrame(13);
}else{
person.setFrame(13);
}
}else if((keyState & this.DOWN_PRESSED)>0){
bgLayer.move(0, -12);
y++;
if(person.getFrame()==1){
person.setFrame(3);
}else if(person.getFrame()==3){
person.setFrame(1);
}else{
person.setFrame(1);
}
}
System.out.println(bgLayer.getX()+":"+bgLayer.getY());
person.move(x, y);
this.checkBounds(bgLayer);
inputDelay=0;
}
}
public void draw(Graphics g){
bgLayer.paint(g);
person.paint(g);
this.flushGraphics();
}
public void checkBounds(TiledLayer layer){
//x
if(layer.getX() > -4){
layer.setPosition(-4, layer.getY());
}else if(layer.getX() < -400){
layer.setPosition(-400, layer.getY());
}
//y
if(layer.getY() > 3){
layer.setPosition(layer.getX(), 3);
}else if(layer.getY() < -189){
layer.setPosition(layer.getX(), -189);
}
}
public void run() {
// TODO Auto-generated method stub
Graphics g=this.getGraphics();
while(!sleeping){
this.update();
this.draw(g);
try {
Thread.sleep(frameDelay);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2.com.sk.form中
package com.sk.view;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sk.model.Wcanvas;
public class WForm extends MIDlet{
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
Wcanvas canvas=new Wcanvas(Display.getDisplay(this));
canvas.start();
}
}
//TiledLayer可以让手机的小屏幕实现滚动式大背景