春节好少休息了一下。所以快一个月没有加东西了。但千万不要以为我偷懒了,已经看完了m3g的部分介绍。
当然,学习还要稳扎稳打,所以,今天把上会剩下的一点完成。让我们的画面动起来!
要点:1、学会使用Sprite, 2、学会使用Thread
Sprite 是Layer的子类,因此,大多数和Layer的用法一样,需要对期显示序列进行手动设置比较让人头疼。直接用线程控制了。
增加了一个线程文件 SperkAni.java
public class SpeakAni extends Thread {
private BackCanvas bc;
private int frameIndex;
private int maxFrameIndex;
public SpeakAni (BackCanvas creater){
bc=creater;
frameIndex=0;
maxFrameIndex=bc.Sore.getRawFrameCount()-1;
}
public void run(){
int WordRound =5;
while (WordRound>0){
while (frameIndex<=maxFrameIndex){
bc.Sore.setFrame(frameIndex);
frameIndex=frameIndex+1;
bc.repaint();
try{
sleep(50);
System.out.println("changeing...");
}
catch (Exception e)
{
;
}
}
frameIndex=0;
WordRound=WordRound-1;
}
}
}
控制一个动画序列连续播放5次,每帧间隔50毫秒。
绘制部分也增加了相应代码,需要注意的问题,已经用注释写出来了
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
public class BackCanvas extends GameCanvas {
private Scence scence;
private LayerManager layerManager;
private TiledLayer backLayer;
public Sprite Sore;
public BackCanvas(){
super(false);
scence = new Scence();
scence.screenX =0;
scence.screenY =0;
Sore=scence.LoadSprite("Sore");
layerManager = new LayerManager();
Image temp= scence.init();
backLayer = new TiledLayer(1,1,temp,temp.getWidth(),temp.getHeight());
backLayer.setCell(0, 0, 1);
layerManager.append(Sore); //添加顺序决定覆盖。
layerManager.append(backLayer);
}
public void paint(Graphics g){
scence.painting =true;
try{
layerManager.setViewWindow(scence.screenX, scence.screenY, scence.screenWidth, scence.screenHeight);
layerManager.paint(g, 0, 20);
if (scence.DEBUG)
System.out.println("paint() Executed!");
}catch (Exception e){
System.out.println("绘制过程出现故障");
}finally{
scence.painting =false;
}
//this.flushGraphics();
}
protected void keyPressed(int keyCode){
SpeakAni ani;
int oldx=scence.screenX;
int oldy=scence.screenY;
try{
if (scence.painting) return;
switch (keyCode) {
case -1:if (scence.screenY>scence.unitHeight) scence.screenY=scence.screenY-scence.unitHeight; break;
case -2:if (scence.screenY<scence.backHeight-scence.screenHeight-scence.unitHeight) scence.screenY=scence.screenY+scence.unitHeight;break;
case -3:if (scence.screenX>scence.unitWidth) scence.screenX=scence.screenX-scence.unitWidth; break;
case -4:if (scence.screenX<scence.backWidth-scence.screenWidth-scence.unitWidth) scence.screenX=scence.screenX+scence.unitWidth;break;
case -5:{ani= new SpeakAni(this);ani.setPriority(1); ani.start();} //过高的现成顺序会导致线程sleep 也优先执行。
}
System.out.println(keyCode);
}catch (Exception e){
System.out.println("捕获不明错误");
scence.screenX=oldx;
scence.screenY=oldy;
}
this.repaint();
if (scence.DEBUG) System.out.println("keyPressed() Executed!");
}
}
其实有时候自己一点一点增加代码有些好处,可以知道哪些是必须的try catch。
主文件没有改动,场景增加了几个功能。
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public class Scence {
public boolean DEBUG=false;
public int StageLevel=0;
public final int screenWidth=200;
public final int screenHeight=300;
public final int ScreenCenX=100;
public final int ScreenCenY=150;
public final int unitWidth=20;
public final int unitHeight=30;
public final int faceWidth=50;
public final int faceHeight=50;
public int screenX=0;
public int screenY=0;
public int backWidth;
public int backHeight;
public boolean painting=false;
public Image init(){
Image temp;
try{
temp= Image.createImage("/Level"+StageLevel+".png");
}catch (Exception e){
System.out.println("读取地图错误。--"+"/Levle"+StageLevel+".png");
return null;
}
backWidth = temp.getWidth();
backHeight= temp.getHeight();
return temp;
}
public Sprite LoadSprite(String name){
Image temp;
Sprite sp;
try{
temp=Image.createImage("/"+name+".png");
}
catch (Exception e){
System.out.println("读取"+name+".png错误");
return null;
}
sp=new Sprite(temp,faceWidth,faceHeight);
sp.setFrameSequence(null);
sp.setPosition(ScreenCenX, ScreenCenY);
return sp;
}
}
先运行一下吧,索尔可以说话了。相应的图片资源。你可以自己做几个png图。名字改成Level0.png; Sore.png就可以了。
如果不使用缓存,那么以上就是绘制的主要部分功能了。至于双缓存,明天再说。先看看索尔在讲什么吧!
250

被折叠的 条评论
为什么被折叠?



