J2me 手机游戏开发,之 sprite(动画)图画精灵(帧动画部分)
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 20.25pt; HEIGHT: 80.25pt" type="#_x0000_t75"><imagedata o:title="tortoise" src="file:///C:%5CDOCUME~1%5CLUCKYS~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>tortoise.png(20*80)
乌龟图:将4个小乌龟图片合成到一个大的乌龟图中,小乌龟(20*20),注意这里,sprite的帧动画,是将所有的相同尺寸的小图像合成到一个大图像中,然后载入大图像,通过setFrame,nextFrame,preFrame等方法来控制显示 哪帧!
GameCanvas代码:
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;
public class TestCanvas extends GameCanvas implements Runnable{
private Display display;
private Sprite sprite;
public TestCanvas(Display display) {
super(true);
// TODO Auto-generated constructor stub
this.display=display;
try {
sprite=new Sprite(Image.createImage("/com/sk/imgs/tortoise.png"),20,20);
//载入大乌龟图,并制定帧(4个小乌龟图)的尺寸
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Load image error");
}
}
public void start(){
display.setCurrent(this);
new Thread(this).start();
}
private void update(){
int keyState = this.getKeyStates();
int x=0;
int y=0;
if((keyState & this.LEFT_PRESSED)!=0){
sprite.setFrame(0); //调用 1帧
x--;
}else if((keyState & this.RIGHT_PRESSED)!=0){
sprite.setFrame(1); //调用 2帧
x++;
}
if((keyState & this.UP_PRESSED)!=0){
sprite.setFrame(2); //调用 3帧
y--;
}else if((keyState & this.DOWN_PRESSED)!=0){
sprite.setFrame(3); // 调用 4帧
y++;
}
sprite.move(x, y);
}
private void draw(Graphics g){
g.setColor(0xffffff);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
sprite.paint(g);
this.flushGraphics();
}
public void run() {
// TODO Auto-generated method stub
Graphics g=this.getGraphics();
for(;;){
this.update();
this.draw(g);
try {
Thread.sleep(33); //休眠33毫秒,即帧速率 30
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3. midlet 调用 canvas
package com.sk.view;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sk.model.TestCanvas;
public class Test 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
TestCanvas canvas=new TestCanvas(Display.getDisplay(this));
canvas.start();
}
}