LayerManager:图层管理器,可以方便的实现多个图层(Sprite,TiledLayer等)的管理
try {
TiledLayer bgLayer=new TiledLayer(20,15,Image.createImage("/com/sk/imgs/bg0.png"),32,32);
Sprite person=new Sprite(Image.createImage("/com/sk/imgs/fighter01.png"),32,48);
TiledLayerbarrier=new TiledLayer(20,15,Image.createImage("/com/sk/imgs/barrier.png"),32,32);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Loading image error");
}
//
LayerManager manager=new LayerManager(); //创建图层管理器,管理器的z-order与web的z-index顺序相反,第一次添加的图层处于最上层,依次向下
manager.append(person);
manager.append(barrier);
manager.append(bgLayer);
xView=(bgLayer.getWidth()-this.getWidth())/2;
yView=(bgLayer.getHeight()-this.getHeight())/2;
manager.setViewWindow(xView, yView, getWidth(), getHeight());
//设置 图层管理器的可视窗口
person.setPosition(xView+(this.getWidth()-person.getWidth())/2, yView+(this.getHeight()-person.getHeight())/2);
int x=0;
for(int i=0;i<15;i++){ //此循环为了填充bgLayer与barrier的单元格
for(int j=0;j<20;j++){
x++;
bgLayer.setCell(j, i, x);
barrier.setCell(j,i, x);
}
}
___________________________________________
具体代码如下:
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.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class Wcanvas2 extends GameCanvas implements Runnable{
private Display display;
private Sprite person;
private TiledLayer bgLayer;
private boolean sleeping;
private int frameDelay=33;
private LayerManager manager;
private TiledLayer barrier;
private int xView,yView;
public Wcanvas2(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);
barrier=new TiledLayer(20,15,Image.createImage("/com/sk/imgs/barrier.png"),32,32);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Loading image error");
}
//
manager=new LayerManager();
manager.append(person);
manager.append(barrier);
manager.append(bgLayer);
xView=(bgLayer.getWidth()-this.getWidth())/2;
yView=(bgLayer.getHeight()-this.getHeight())/2;
manager.setViewWindow(xView, yView, getWidth(), getHeight());
//
person.setPosition(xView+(this.getWidth()-person.getWidth())/2, yView+(this.getHeight()-person.getHeight())/2);
int x=0;
for(int i=0;i<15;i++){
for(int j=0;j<20;j++){
x++;
bgLayer.setCell(j, i, x);
barrier.setCell(j,i, x);
}
}
display.setCurrent(this);
sleeping=false;
new Thread(this).start();
}
public void stop(){
sleeping=true;
}
public void update(){
int keyState=this.getKeyStates();
int x=0,y=0;
if((keyState & this.LEFT_PRESSED) !=0){
x-=4;
}else if((keyState & this.RIGHT_PRESSED) !=0){
x+=4;
}
if((keyState & this.UP_PRESSED) !=0){
y-=4;
}else if((keyState & this.DOWN_PRESSED) !=0){
y+=4;
}
if(x!=0 || y !=0){
person.move(x, y);
xView+=x;
yView+=y;
manager.setViewWindow(xView, yView, this.getWidth(), this.getHeight());
System.out.println(xView+":"+yView);
}
//
//if(person.collidesWith(barrier, true)){
//manager.setViewWindow(xView, yView, getWidth(), getHeight()); //后退
//
//person.move(-x, -y);
//
//}else{
//xView+=x;
//yView+=y;
//}
}
public void draw(Graphics g){
g.fillRect(0, 0, this.getWidth(), this.getHeight());
manager.paint(g, 0, 0);//画出manager
this.flushGraphics();//情况缓冲区
}
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.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sk.model.Wcanvas2;
public class WForm2 extends MIDlet implements CommandListener{
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
Command cmd_exit=new Command("退出",Command.EXIT,1);
Wcanvas2 canvas=new Wcanvas2(Display.getDisplay(this));
canvas.addCommand(cmd_exit);
canvas.setCommandListener(this);
canvas.start();
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c.getCommandType()==Command.EXIT){
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.notifyDestroyed();
}
}
}
//
利用LayerManager的setViewWindow(xView, yView, this.getWidth(), this.getHeight())方法实现手机屏幕在大背景上滚动