WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发J2ME RPG游戏边学边做(二)

J2ME RPG游戏边学边做(二)
作者:zhagy  来源:J2MEDEV  发布时间:2005-8-27 15:11:21
id startup()
{
this.sign = true;
try
{
backgroundMap = Scene.createTiledLayerByBackground(
Image.createImage("/background.png"));
upimage = Image.createImage("/hero_up.png");
downimage = Image.createImage("/hero_down.png");
leftimage = Image.createImage("/hero_left.png");
rightimage = Image.createImage("/hero_right.png");
//创建Layer管理视图类
braveManager = new BraveManager();
braveManager.setBraveCanvas(this);

hero = new Hero(upimage, 17, 26);
hero.setFrameSequence(new int[]{1, 1, 0, 0, 1, 1, 2, 2});
hero.setBraveCanvas(this);
hero.setBraveManager(braveManager);
hero.init(0,0);

}
catch(Exception e)
{
e.printStackTrace();
}
Thread thread = new Thread(this);
thread.start();
}

public void run()
{
g = getGraphics();
//插入图层
braveManager.insert(hero, 0);
braveManager.insert(backgroundMap, 1);
while(sign)
{
try
{
input(g);
paint(g);
Thread.sleep(15);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

public void input(Graphics g) throws IOException
{
int keystates = getKeyStates();
switch(keystates)
{
case UP_PRESSED:
hero.moveUp(upimage);
break;
case DOWN_PRESSED:
hero.moveDown(downimage);
break;
case LEFT_PRESSED:
hero.moveLeft(leftimage);
break;
case RIGHT_PRESSED:
hero.moveRight(rightimage);
break;
}
hero.afresh();
//刷新视图的位置
braveManager.afresh();
}

public void paint(Graphics g)
{
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffffff);
//显示视图
braveManager.paint(g, 0, 0);
flushGraphics();
}
}


这样英雄就可以在地图上任何地方行动了,不过还得改一个小地方:
不知道大家还记的不,在Hero类中,我们定义英雄移动的范围最大为屏幕的尺寸。
这显然是不行的,最大的移动范围应改成地图的大小:
this.y = Math.min(braveManager.getLayerAt(1).getHeight(), y + 1);
this.x = Math.min(braveManager.getLayerAt(1).getWidth(), x + 1);

代码如下:

Hero.java

package brave;

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;

public class Hero extends Sprite
{
private int x;
private int y;

private BraveCanvas braveCanvas;
private BraveManager braveManager;

public Hero(Image image, int frameWidth, int frameHeight)
{
super(image, frameWidth, frameHeight);
}

public void setBraveCanvas(BraveCanvas braveCanvas)
{
this.braveCanvas = braveCanvas;
}

public void setBraveManager(BraveManager braveManager)
{
this.braveManager = braveManager;
}

public void setManager(BraveCanvas braveCanvas)
{
this.braveCanvas = braveCanvas;
}

public void init(int x, int y)
{
this.x = x;
this.y = y;
}

public void afresh()
{
setPosition(this.x, this.y);
}

public void moveUp(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
this.y = Math.max(0, y - 1);
}

public void moveDown(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
this.y = Math.min(braveManager.getLayerAt(1).getHeight(), y + 1);
}

public void moveLeft(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
this.x = Math.max(0, x - 1);

}

public void moveRight(Image image) throws IOException
{
setImage(image, 17, 26);
nextFrame();
this.x = Math.min(braveManager.getLayerAt(1).getWidth(), x + 1);
}
}

现在英雄的移动范围大了,但只英雄一人,也太孤单了,我们给他创造一个小镇吧。
修改Scene类如下:

Scene.java
package brave;

import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.lcdui.Image;

public class Scene
{
public static TiledLayer createTiledLayerByBackground(Image image)
{
TiledLayer tiledLayer = new TiledLayer(10, 8, image, 48, 64);
tiledLayer.fillCells(0, 0, 10, 8, 2);
return tiledLayer;
}

public static TiledLayer createTiledLayerByForeground(Image image)
{
TiledLayer tiledLayer = new TiledLayer(30, 32, image, 16, 16);
// 30 * 32
int[] maplist =
{
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28 29
0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0

,0 ,0 ,0 ,0 ,0 ,//0
0

,28,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,30,0 ,//1
0 ,34,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0

,0 ,0 ,0 ,36,0 ,//2
0 ,34,0 ,1 ,2 ,3 ,4 ,5 ,0 ,0 ,0 ,1 ,2 ,3 ,3 ,26,3 ,3 ,4 ,5 ,0 ,0 ,1 ,2 ,3

,4 ,5 ,0 ,36,0 ,//3
0 ,34,0 ,7 ,8 ,46,10,11,0 ,0 ,0 ,7 ,8 ,47,31,32,33,47,10,11,0 ,0 ,7 ,8

,46,10,11,0 ,36,0 ,//4
0 ,34,0 ,13,14,15,16,17,0 ,0 ,0 ,13,14,14,37,38,39,14,16,17,0 ,0

,13,14,15,16,17,0 ,36,0 ,//5
0 ,34,0 ,19,20,21,22,23,6 ,0 ,0 ,19,20,20,43,44,45,20,20,23,0 ,0

,19,20,21,22,23,0 ,36,0 ,//6
0 ,34,0 ,0 ,0 ,12,0 ,0 ,0 ,0 ,0 ,24,24,24,13,15,17,24,24,24,0 ,0

上一页  [1] [2] [3] [4]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码