站内搜索:     
站点首页破釜学院网页制作Flash游戏制作 → Tile Based Game AS2
正在加载相关信息.....
Web 站内搜索
Tile Based Game AS2
】【打印】【加入收藏】【关闭收藏到新浪ViVi】【收藏到365KEY】 浏览字号:
日期:2005-09-14 人气: 出处:闪吧 作者: sidealice
  声明:本程序是向Klas Kroon的Tile Based Game借鉴改写的。原来的程序是AS1/FLASH5的,本次发布的在AS2/FLASH8下通过。
测试中发现除部分寻路出现偏差外,基本令人满足。
640*480的SLG MAP:
点击浏览该文件
大小4KB

/**
+---------------------------------------------------------+
| |
| Game class |
| version 0.02 |
| 2005.9.8 |
| 作者: af |
| http://www.sound.jp/airforce/ |
| Email:sidealice@yahoo.com.cn |
| |
|=========================================================|
| |
| 全局变量: |
| _global.mapW 地图宽 |
| _global.mapH 地图高 |
| _global.ntileY Tile在Y方向个数 |
| _global.ntileX Tile在X方向个数 |
| _global.tileW Tile长 |
| _global.tileH Tile宽 |
| _global.rootPath "_root" |
| _global.mapPath "_root.map" | |
| _global.tilePath "_root.map.tile"*未用 |
| _global.charPath "_root.map.char" |
| _global.clicked 鼠标点击 |
| _global.targetx 目标Tile的X格数 |
| _global.targety 目标Tile的Y格数 |
| _global.charMoving 角色移动 |
| |
+---------------------------------------------------------+ 


*/
class game extends Object {
//
//--------------------成员定义--------------------
public var gameMap;
public var gamePath:path;
public var gameMainChar:char;
//public var gameEnemyList;
public var xmouse:Number, ymouse:Number;
//
//--------------------定义结束--------------------
//
//
//--------------------构造函数--------------------
//开始游戏
//
public function game() {
init();
var myMap1 = 
[
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1], 
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,1,0,0,1], 
[1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1,0,0,0,0,0,1,0,0,1], 
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,0,1,0,1,1,1,0,0,1], 
[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,1,1,0,0,1,0,0,0,1], 
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1,0,0,0,0,0,0,1,0,1], 
[1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,0,0,0,0,0,0,1,0,1], 
[1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,1,1,0,0,0,0,1,0,1], 
[1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0,1,1,0,0,1,0,1,0,1], 
[1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,0,1,1,1,1,0,1,0,1], 
[1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0,0,0,0,0,0,0,0,0,1], 
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,0,1,0,1,1,1,0,0,1], 
[1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0,0,1,0,1,1,1,0,1,1], 
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,0,0,0,0,1,1,0,0,1], 
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,1,1,0,1,1,1,0,0,1], 
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1]
];
this.gameMap = new map("game");
this.gameMap.loadMap(myMap1);
this.gameMap.drawMap();
this.gameMainChar = new char("Af");
this.gameMainChar.drawChar(2,1);

}
//==================================================
//--------------------init()--------------------
//初始化全局变量
//
public function init() {
_global.tileW = 30;
_global.tileH = 30;
_global.rootPath = "_root";
_global.mapPath = "_root.map";
_global.tilePath = "_root.map.tiles";
_global.charPath = "_root.map.char";
_global.clicked = false;
}
//==================================================
//--------------------getTarget()--------------------
//获得鼠标点击的目标格数
//
public function getTarget() {
this.gamePath = new path();
var tempPath = eval(this.gameMap.mapPath);
if (tempPath["t_" + this.ymouse + "_" + this.xmouse].walkable) {
// update target tile
_global.targetx = this.xmouse;
_global.targety = this.ymouse;
// if moving, wait until center is reached
if (!_global.charMoving) {
this.gamePath.findPath(this.gameMainChar.xtile,this.gameMainChar.ytile,_global.targetx,_global.targety);
}
else {
_global.clicked = true;
}
}
}
//==================================================
//--------------------work()--------------------
//鼠标事件的检查,更新位置
//
public function work() {

this.xmouse = Math.round((_root._xmouse - _global.tileW / 2) / _global.tileW);
this.ymouse = Math.round((_root._ymouse - _global.tileH / 2) / _global.tileH);
_root.mouse._x = this.xmouse * _global.tileW;
_root.mouse._y = this.ymouse * _global.tileH;
if (!this.gameMainChar) {
this.gameMainChar.gotoAndStop(1);
}
else {
this.gameMainChar.move(this.gamePath);
this.gameMainChar.play();
}
}
//==================================================
}



/**
+---------------------------------------------------------+
| |
| Char class |
| version 0.02 |
| 2005.9.8 |
| 作者: af |
| http://www.sound.jp/airforce/ |
| Email:sidealice@yahoo.com.cn |
| |
|=========================================================|
| |
| 全局变量: |
| _global.mapW 地图宽 |
| _global.mapH 地图高 |
| _global.ntileY Tile在Y方向个数 |
| _global.ntileX Tile在X方向个数 |
| _global.tileW Tile长 |
| _global.tileH Tile宽 |
| _global.rootPath "_root" |
| _global.mapPath "_root.map" | |
| _global.tilePath "_root.map.tile"*未用 |
| _global.charPath "_root.map.char" |
| _global.clicked 鼠标点击 |
| _global.targetx 目标Tile的X格数 |
| _global.targety 目标Tile的Y格数 |
| _global.charMoving 角色移动 |
| |
+---------------------------------------------------------+

*/
class char extends MovieClip {
//
//--------------------成员定义--------------------
public var xtile:Number, ytile:Number;
public var dirx:Number = 0, diry:Number = 0;
public var x:Number = 0, y:Number = 0;
public var speed:Number = 2;
public var charName:String;
//
//--------------------定义结束--------------------
//
//
//--------------------构造函数--------------------
//创造角色
//
public function char(cName:String) {
this.charName = cName;
eval(_global.mapPath).attachMovie("char",cName,100000);
_global.charPath = eval(_global.mapPath + "." + this.charName);
}
//==================================================
//--------------------drawChar()--------------------
//drawChar(xtile:Number, ytile:Number)
//
public function drawChar(nxtile:Number, nytile:Number) {
this.xtile = nxtile;
this.ytile = nytile;
_global.charPath._x = (this.xtile * _global.tileW) + _global.tileW / 2;
_global.charPath._y = (this.ytile * _global.tileW) + _global.tileW / 2;
this._x = this.x = _global.charPath._x;
this._y = this.y = _global.charPath._y;
//eval(_global.charPath).gotoAndStop(1);
_global.charMoving = false;
}
//==================================================
//--------------------moveChar()--------------------
//moveChar(pather:path)
//
public function move(pather) {
if ((this.x - _global.tileW / 2) % _global.tileW == 0 and (this.y - _global.tileH / 2) % _global.tileH == 0) {
// calculate the tile where chars center is
this.xtile = Math.floor(this.x / _global.tileW);
this.ytile = Math.floor(this.y / _global.tileH);
if (_global.clicked) {
_global.clicked = false;
pather.findPath(this.xtile,this.ytile,_global.targetx,_global.targety);
return;
}
if (pather.newPath.length > 0) {
_global.targety = pather.newPath.pop();
_global.targetx = pather.newPath.pop();
// right
if (_global.targetx > this.xtile) {
this.dirx = 1;
this.diry = 0;
// left
}
else if (_global.targetx < this.xtile) {
this.dirx = -1;
this.diry = 0;
// up
}
else if (_global.targety > this.ytile) {
this.dirx = 0;
this.diry = 1;
// down
}
else {
this.dirx = 0;
this.diry = -1;
}
}
else {
_global.charMoving = false;
return;
}
}
// move
this.y += this.speed * this.diry;
this.x += this.speed * this.dirx;
// update char position
this._x = eval(_global.charPath)._x = this.x;
this._y = eval(_global.charPath)._y = this.y;
// face the direction
eval(_global.charPath).gotoAndStop(this.dirx + this.diry * 2 + 3);
}
}



/**
+---------------------------------------------------------+
| |
| Path class |
| version 0.02 |
| 2005.9.8 |
| 作者: af |
| http://www.sound.jp/airforce/ |
| Email:sidealice@yahoo.com.cn |
| |
|=========================================================|
| |
| 全局变量: |
| _global.mapW 地图宽 |
| _global.mapH 地图高 |
| _global.ntileY Tile在Y方向个数 |
| _global.ntileX Tile在X方向个数 |
| _global.tileW Tile长 |
| _global.tileH Tile宽 |
| _global.rootPath "_root" |
| _global.mapPath "_root.map" | |
| _global.tilePath "_root.map.tile"*未用 |
| _global.charPath "_root.map.char" |
| _global.clicked 鼠标点击 |
| _global.targetx 目标Tile的X格数 |
| _global.targety 目标Tile的Y格数 |
| _global.charMoving 角色移动 |
| |
+---------------------------------------------------------+

*/
class path extends Array {
//
//--------------------成员定义--------------------
//
//
public var rootPath, mapPath, tilePath, charPath;
public var done:Boolean = false, visited:Boolean = true;
public var nodename:String;
public var cost:Number;
public var x:Number, y:Number;
public var parentx:Number, parenty:Number;
public var newPath:Array;
public var pathTime;
public var Unchecked_Neighbours;
//
//--------------------定义结束--------------------
//
//
//--------------------构造函数--------------------
//
//
public function path() {
}
//==================================================
//----------Function findPath()----------
//findPath(startx,starty,targetx,targety)
//
public function findPath(startx, starty, targetx, targety) {
clearMark();
// start timer
// make new array for unchecked tiles
this.Unchecked_Neighbours = [];
// not done yet
this.done = false;
// initialize
this.nodename = "node_" + starty + "_" + startx;
// create first node
var cost = Math.abs(startx - targetx) + Math.abs(starty - targety);
this[this.nodename] = {x:startx, y:starty, visited:true, parentx:null, parenty:null, cost:cost};
// add node to array
this.Unchecked_Neighbours[this.Unchecked_Neighbours.length] = this[this.nodename];
// loop
while (this.Unchecked_Neighbours.length > 0) {
// get the next node
var N = this.Unchecked_Neighbours.shift();
// we’ve finished
if (N.x == targetx and N.y == targety) {
// done
make_path(N);
this.done = true;
break;
}
else {
N.visited = true;
// right neighbour
addNode(N,N.x + 1,N.y,targetx,targety);
// left neighbour
addNode(N,N.x - 1,N.y,targetx,targety);
// up neighbour
addNode(N,N.x,N.y + 1,targetx,targety);
// down neighbour
addNode(N,N.x,N.y - 1,targetx,targety);
}
}
// remove path object
delete this;
// check if path was found
if (this.done) {
// we reached the goal
return true;
}
else {
// we couldn’t get there;
return false;
trace("we couldn’t get there;");
}
}
//==================================================
//--------------------make_path()--------------------
//make_path(ob)
//
public function make_path(ob) {
// create path array
this.newPath = new Array();
// loop until no more parents
while (ob.parentx != null) {
// add the x and y
this.newPath[this.newPath.length] = ob.x;
this.newPath[this.newPath.length] = ob.y;
// mark the tile
eval(_global.mapPath)["t_" + ob.y + "_" + ob.x].mark.gotoAndStop(2);
// make its parent new object
ob = this["node_" + ob.parenty + "_" + ob.parentx];
}
// start moving again
_global.charMoving = true;
}
//==================================================
//--------------------addNode()--------------------
//addNode(ob, x, y, targetx, targety)
//
public function addNode(ob, x, y, targetx, targety) {
// name of the new node
this.nodename = "node_" + y + "_" + x;
// add to the unchecked neighbours, if its walkable
if (eval(_global.mapPath)["t_" + y + "_" + x].walkable) {
// find estimated cost to the target
this.cost = Math.abs(x - targetx) + Math.abs(y - targety);
// and if its not visited yet
if (this[this.nodename].cost > cost or this[this.nodename].cost == undefined) {
// make new node
this[this.nodename] = {x:x, y:y, visited:false, parentx:ob.x, parenty:ob.y, cost:cost};
// add to the array
for (var i = 0; i < this.Unchecked_Neighbours.length; i++) {
if (cost < this.Unchecked_Neighbours[i].cost) {
this.Unchecked_Neighbours.splice(i,0,this[this.nodename]);
break;
}
}
// didnt add it yet, too much cost, add to the end
if (i >= this.Unchecked_Neighbours.length) {
this.Unchecked_Neighbours[this.Unchecked_Neighbours.length] = this[this.nodename];
}
}
}
}
//==================================================
//--------------------clearMark()--------------------
//
public function clearMark() {
for (var i = 0; i < _global.ntileY; ++i) {
for (var j = 0; j < _global.ntileX; ++j) {
eval(_global.mapPath)["t_" + i + "_" + j].mark.gotoAndStop(1);
}
}
}
}
/*
*/



/**
+---------------------------------------------------------+
| |
| Map class |
| version 0.02 |
| 2005.9.8 |
| 作者: af |
| http://www.sound.jp/airforce/ |
| Email:sidealice@yahoo.com.cn |
| |
|=========================================================|
| |
| 全局变量: |
| _global.mapW 地图宽 |
| _global.mapH 地图高 |
| _global.ntileY Tile在Y方向个数 |
| _global.ntileX Tile在X方向个数 |
| _global.tileW Tile长 |
| _global.tileH Tile宽 |
| _global.rootPath "_root" |
| _global.mapPath "_root.map" | |
| _global.tilePath "_root.map.tile"*未用 |
| _global.charPath "_root.map.char" |
| _global.clicked 鼠标点击 |
| _global.targetx 目标Tile的X格数 |
| _global.targety 目标Tile的Y格数 |
| _global.charMoving 角色移动 |
| |
+---------------------------------------------------------+

*/
class map extends MovieClip {
//
//--------------------成员定义--------------------
//
//
public var mapW:Number, mapH:Number;
public var tileW:Number, tileH:Number;
public var rootPath, mapPath, tilePath, charPath;
public var walkable:Boolean = true;
public var mapData = [[]];
//
//--------------------定义结束--------------------
//
//
//--------------------构造函数--------------------
//
//
public function map(sName:String) {
this.tileW=_global.tileW
this.tileH=_global.tileH
this.rootPath=_global.rootPath
this.mapPath=_global.mapPath
this.tilePath=_global.tilePath
this.charPath=_global.charPath
_root.createEmptyMovieClip(sName,_root.getNextHighestDepth());
_global.mapPath = this.mapPath = this.rootPath + "." + sName;
}
//==================================================
//--------------------loadMap()--------------------
//loadMap(tempMap)
//tempMap=[[]]->2D Array
//
public function loadMap(tempMap) {
this.mapData = tempMap;
this.mapW = this.mapData[0].length * this.tileW;
this.mapH = this.mapData.length * this.tileH;
_global.mapW = this.mapW;
_global.mapH = this.mapH;
_global.ntileY = this.mapData.length;
_global.ntileX = this.mapData[0].length;
//tracepro(mapData,"mapData");
}
//==================================================
//--------------------drawMap()--------------------
//attachMovie(id,newName,depth)
//
public function drawMap() {
var tempPath = eval(_global.mapPath)
for (var i = 0; i < this.mapData.length; ++i) {
for (var j = 0; j < this.mapData[0].length; ++j) {
var tilename = "t_" + i + "_" + j;
tempPath.attachMovie("tile",tilename,i * 100 + j * 2);
tempPath[tilename]._x = j * this.tileW;
tempPath[tilename]._y = i * this.tileH;
tempPath[tilename].gotoAndStop(this.mapData[i][j] + 1);
if (this.mapData[i][j] == 1) {
tempPath[tilename].walkable = false;
}
else {
tempPath[tilename].walkable = true;
}
}
}
}
//==================================================
//--------------------TraceBack--------------------
//
public function tracepath() {
trace("-----------trace path start-----------");
trace("rootPath: " + rootPath);
trace("mapPath: " + mapPath);
trace("tilePath: " + tilePath);
trace("-----------trace path end-----------");
}
public function tracepro(obj:Object, objName:String) {
trace("-----------trace property start-----------");
trace("|Object Name:" + objName);
for (var prop in obj) {
trace(objName + "." + prop + " = " + obj[prop]);
}
trace("-----------trace property end-----------");
}
}
/**

保留使用:
public function set frame(n:Number) {
this.gotoAndStop(n + 1);
switch (n) {
case 0 :
this.walkable = true;
trace("Set to wall!");
break;
case 1 :
this.walkable = false;
trace("Set to floor!");
break;
default :
trace("Wraning: no frame number to set!");
}
}
public function get frame() {
}
**/

>>>> 进入论坛交流 <<<<

相关文章:
暂时没有相关文章