import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
public class LayerCanvas extends GameCanvas implements Runnable {
private Sprite buzhi = null;
private Sprite koy=null;
private Sprite fire = null;
//koy等待桢
int[] koyWait = { 16, 0 };
//buzhi等待桢
int[] buzhiWait = { 29 };
//fire等待桢
int[] fireFrame = { 0, 1, 2, 3, 4, 3, 2, 1, 0 };
public LayerCanvas(boolean suppressKeyEvents) {
super(suppressKeyEvents);
setFullScreenMode(true);
//加载Koy图片桢
Image image1 = createImage("/Koy.png");
koy = new Sprite(image1, image1.getWidth()/17, image1.getHeight());
koy.setPosition(0,(this.getHeight()- koy.getHeight()-50));
koy.setFrame(0);
koy.setTransform(Sprite.TRANS_MIRROR);
koy.setFrameSequence(koyWait);
//加载buzhi图片桢
Image image2 = createImage("/buzhi.png");
buzhi = new Sprite(image2, image2.getWidth()/6, image2.getHeight()/5);
buzhi.setPosition(this.getWidth(), (this.getHeight()- buzhi.getHeight()-50));
buzhi.setFrame(0);
buzhi.setTransform(Sprite.TRANS_MIRROR);
buzhi.setFrameSequence(buzhiWait);
//加载fire图片桢
Image image3 = createImage("/fire.png");
fire = new Sprite(image3, image3.getWidth() / 5, image3.getHeight());
fire.setPosition(0, (this.getHeight() - fire.getHeight() - 50));
fire.setFrameSequence(fireFrame);
new Thread(this).start();
}
private static Image createImage(String file){
Image image = null;
try{
image = Image.createImage(file);
}catch(Exception e){
e.printStackTrace();
}
return image;
}
public void paint(Graphics g){
g.setColor(0x0000000);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
buzhi.paint(g);
buzhi.nextFrame();
koy.paint(g);
koy.nextFrame();
koy.setTransform(Sprite.TRANS_NONE);
this.flushGraphics();
}
public void run() {
Graphics g = getGraphics();
boolean isMoveFire = false;
while(true){
long st = System.currentTimeMillis();
paint(g);
int et = (int)(System.currentTimeMillis() - st);
int keyState = getKeyStates();
if ((keyState & FIRE_PRESSED) != 0)
{
koy.setFrameSequence(null);
}
//koy如果到了第6桢,发射fire
if (koy.getFrame() == 6) {
isMoveFire = true;
}
//fire移动绘制
if (isMoveFire) {
fire.paint(g);
fire.setVisible(true);
fire.nextFrame();
this.flushGraphics();
fire.move(12, 0);
}
//检测fire与buziho是否碰撞
if(fire.collidesWith(buzhi, true))
{
fire.setVisible(false);
buzhi.setFrameSequence(null);
fire.setPosition(0, (this.getHeight() - fire.getHeight() - 50));
isMoveFire = false;
}
//koy到了16桢时,待命
if (koy.getFrame() == 16) {
koy.setFrameSequence(koyWait);
}
////buzhi到了29桢时,待命
if (buzhi.getFrame() == 29){
buzhi.setFrameSequence(buzhiWait);
}
if(et < 120){
try {
Thread.sleep(120 - et);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一次课外作业: 程序是个动画演示程序,动画设计为一开始屏幕上有两个人物,一个是koy, 一个是buzhi, 两个人对面相站, 分别在屏幕的最左边和最右边,koy在左, buzhi在右, 两个人一开始处于站立待命状态, 当玩家按下fire键后, koy 向buzhi发出一个飞行道具fire, 飞行道具从koy处飞向buzhi, 当飞行道具接触到buzhi后,飞行道具消失并且buzhi开始做甩扇子动作, 动作完毕又回到待命状态.如此循环下去.只要玩家按下fire键, 就会完成上面的动画. 详细说明: 屏幕:320X240, 竖版,背景色黑色 人物定义: Koy: 图片Koy.png 动作定义:待命:16号帧和0号帧循环播放 发射飞行道具:1号帧至15号帧, 发射一个飞行道具只播放一次, 特别注意飞行道具是6号帧时才飞出去. Buzhi:图片 buzhi.png 动作定义: 待命:29号帧循环播放 受到攻击:0号帧至28号帧, 受到一次攻击就播放一次 Fire: 图片fire.png 动作定义:飞行:以{0,1,2,3,4,3,2,1,0}的序列播放.
资源推荐
资源详情
资源评论
收起资源包目录
SpriteTest.rar (16个子文件)
SpriteTest
SpriteTest.jad 199B
LayerMIDlet.java 683B
.classpath 244B
LayerCanvas.java 3KB
.settings
org.eclipse.jdt.core.prefs 166B
.eclipseme 222B
buzhi.png 10KB
.project 542B
fire.png 3KB
LayerCanvas.class 3KB
deployed
Koy.png 7KB
.eclipseme.tmp
verified
classes
LayerCanvas.class 3KB
LayerMIDlet.class 996B
libs
emulation
SpriteTest.jad 223B
SpriteTest.jar 22KB
LayerMIDlet.class 996B
共 16 条
- 1
资源评论
Ocneman
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功