| | |
|
组别 | 校尉 |
级别 | 裨将军 |
功绩 | 13 |
帖子 | 310 |
编号 | 347822 |
注册 | 2009-11-21 |
| |
| | |
|
|
|
下面实现地图跳转
首先,先把地图图片扩展一下
将stage.jpg替换如下
然后,Map001.as中读取地图小位图的时候,需要变更一下:
public function drowMap():void{
//地图清空
BaseRemove.removeAllChildren(_mapFrame);
//地图重绘
for(var i:int= _mPoint.y + _mapStartY;i<_mPoint.y + _mapEndY + 10;i++){
for(var j:int=_mPoint.x + _mapStartX;j<_mPoint.x + _mapEndX + 10;j++){
var index:int = _map[j];
var indexX:int = Math.floor(index /10);
var indexY:int = index - indexX*10;
__image = new Bitmap(_mapList[indexX][indexY]);
__image.x = (j - _mPoint.x)*48;
__image.y = (i - _mPoint.y)*48;
_mapFrame.addChild(__image);
}
}
}
接下来,准备就是画两个地图了,为了以后添加地图和人物方便,不用改程序,将xml改进一下
FlashProject\data下,先建立一个maps.xml文件,内容如下
<data>
<list>map001</list>
<list>map002</list>
</data>
封装所有地图名称,这次暂时用到两个map001.xml和map002.xml
map001.xml内容:
<data>
<imageMap>
<list>18,18,18,18,18,18,18,18,18,18,18,18,55,55,18</list>
<list>18,18,18,17,17,17,17,17,17,17,17,17,55,55,18</list>
<list>18,18,17,17,17,17,18,18,17,17,17,17,55,55,18</list>
<list>18,17,17,17,18,18,18,18,18,17,17,55,55,17,18</list>
<list>18,17,17,18,22,23,23,23,24,18,17,55,55,17,18</list>
<list>18,17,17,18,25,28,26,79,27,18,55,55,17,17,18</list>
<list>18,17,17,17,17,10,11,12,18,18,55,55,17,17,18</list>
<list>18,17,17,17,17,77,16,78,18,18,55,55,17,17,18</list>
<list>18,17,17,17,17,77,16,78,17,55,55,17,17,17,18</list>
<list>18,18,17,17,10,16,16,16,11,55,55,17,17,17,18</list>
<list>18,18,17,17,77,16,16,16,16,21,21,17,17,17,18</list>
<list>18,18,17,17,13,14,14,14,15,55,55,17,17,17,18</list>
<list>18,18,17,17,55,55,55,55,55,55,55,17,17,18,18</list>
<list>18,18,17,17,55,55,55,55,55,55,55,17,18,18,18</list>
<list>18,18,55,55,55,55,55,55,55,55,55,18,18,18,18</list>
</imageMap>
<dataMap maxX="15" maxY="15">
<list>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</list>
<list>1,1,1,0,0,0,0,0,0,0,0,0,1,1,1</list>
<list>1,1,0,0,0,0,1,1,0,0,0,0,1,1,1</list>
<list>1,0,0,0,1,1,1,1,1,0,0,1,1,0,1</list>
<list>1,0,0,1,1,1,1,1,1,1,0,1,1,0,1</list>
<list>1,0,0,1,1,1,0,1,1,1,1,1,0,0,1</list>
<list>1,0,0,0,0,0,0,0,1,1,1,1,0,0,1</list>
<list>1,0,0,0,0,0,0,0,1,1,1,1,0,0,1</list>
<list>1,0,0,0,0,0,0,0,0,1,1,0,0,0,1</list>
<list>1,1,0,0,0,0,0,0,0,1,1,0,0,0,1</list>
<list>1,1,0,0,0,0,0,0,0,0,0,0,0,0,1</list>
<list>1,1,0,0,0,0,0,0,0,1,1,0,0,0,1</list>
<list>1,1,0,0,1,1,1,1,1,1,1,0,0,1,1</list>
<list>1,1,0,0,1,1,1,1,1,1,1,0,1,1,1</list>
<list>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</list>
</dataMap>
<hero>
<Image>images/player/liubei.gif</Image>
<Coordinate>12,12</Coordinate>
</hero>
<changeMap>
<list name="map002" coordinate="5,9">6,5</list>
</changeMap>
</data>
map002.xml内容:
<data>
<imageMap>
<list>1,2,2,2,2,2,2,2,2,1</list>
<list>1,3,5,5,1,5,5,5,5,1</list>
<list>1,80,4,4,1,80,4,4,4,1</list>
<list>1,80,4,4,1,80,8,7,8,1</list>
<list>1,80,4,4,5,81,4,4,4,1</list>
<list>1,2,2,2,6,4,4,4,4,1</list>
<list>1,3,5,5,81,4,4,4,4,1</list>
<list>1,80,4,4,4,4,4,4,4,1</list>
<list>1,80,4,4,4,4,4,4,9,1</list>
<list>1,2,2,2,2,6,2,2,2,1</list>
</imageMap>
<dataMap maxX="10" maxY="10">
<list>1,1,1,1,1,1,1,1,1,1</list>
<list>1,0,0,0,1,0,0,0,0,1</list>
<list>1,0,0,0,1,0,0,0,0,1</list>
<list>1,0,0,0,1,0,0,1,0,1</list>
<list>1,0,0,0,0,0,0,0,0,1</list>
<list>1,1,1,1,0,0,0,0,0,1</list>
<list>1,0,0,0,0,0,0,0,0,1</list>
<list>1,0,0,0,0,0,0,0,0,1</list>
<list>1,0,0,0,0,0,0,0,1,1</list>
<list>1,1,1,1,1,0,1,1,1,1</list>
</dataMap>
<hero>
<Image>images/player/liubei.gif</Image>
<Coordinate>5,9</Coordinate>
</hero>
<changeMap>
<list name="map001" coordinate="6,5">5,9</list>
</changeMap>
</data>
地图xml中,第一个节点,是地图图片信息,第二个节点是地形信息,
第三个节点是人物信息,Image为人物图片,Coordinate为人物出现的默认坐标
第四个节点是地图跳转信息,list中的(5,9)是人物到达指定的跳转位置坐标,name指跳转后的地图名称,coordinate是人物出现在跳转后地图的坐标。
以后如果想另添加一个新的场景,只需要添加一个map003.xml,然后在maps.xml添加相应的节点就行了
GameStart.as中,将相应地图名称封装,变更如下:
package com{
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.geom.Point;
public class GameStart extends MovieClip {
private var _mapList:HashMap = new HashMap();
private var _mapName:String = "map001";
//人物跳转坐标
private var _mapPoint:Point;
public function GameStart() {
var loader:URLLoader = new URLLoader(new URLRequest("data/maps.xml"));
loader.addEventListener(Event.COMPLETE,initMap);
}
public function initMap(evet:Event):void{
//得到xml信息
var mapXml:XML = new XML(evet.target.data);
//装载所有地图信息
for each ( var datement:XML in mapXml.elements( ) ) {
var strDate:String = ("" + datement);
_mapList.put(strDate,"data/" + strDate + ".xml");
}
selectMap();
}
//地图跳转
public function selectMap():void{
BaseRemove.removeAllChildren(this);
//通过名称显示相应地图
var mapValue:String = "" +_mapList.getValue(_mapName);
var map_001:Map001 = new Map001(mapValue,this);
addChild(map_001);
}
//设定地图名称
public function setMapName(mapName:String):void{
_mapName = mapName;
}
//设定人物跳转坐标
public function setPoint(mapPoint:Point = null):void{
_mapPoint = mapPoint;
}
//得到人物跳转坐标
public function getPoint():Point{
return _mapPoint;
}
}
}
Map001.as 中,增加响应解析xml的代码,
public function initMap(evet:Event):void{
//得到xml信息
var mapXml:XML = new XML(evet.target.data);
//得到人物图片
_propleImage = mapXml.hero.Image;
//取得地图最大size
_maxX = mapXml.dataMap.@maxX;;
_maxY = mapXml.dataMap.@maxY;
//得到人物坐标信息
var peoplePoint:Point = _gameMap.getPoint();
if(peoplePoint == null){
var strPoint:String = mapXml.hero.Coordinate;
var pointArray:Array = strPoint.split( ",");
_peoplePoint = new Point(pointArray[0],pointArray[1]);
}else{
_peoplePoint = peoplePoint;
}
//计算人物坐标和显示位置
var mPointX:int = 0;
var mPointY:int = 0;
if(_peoplePoint.x > 5){
if(_peoplePoint.x + 5 <= _maxX){
mPointX = _peoplePoint.x - 5;
_peoplePoint.x = 5;
}else{
mPointX = _maxX - 10;
if(mPointX > 0){
_peoplePoint.x = _peoplePoint.x - mPointX;
}
}
}
if(_peoplePoint.y > 5){
if(_peoplePoint.y + 5 <= _maxY){
mPointY = _peoplePoint.y - 5;
_peoplePoint.y = 5;
}else{
mPointY = _maxY - 10;
if(mPointY > 0 ){
_peoplePoint.y = _peoplePoint.y - mPointY;
}
}
}
_mPoint = new Point(mPointX,mPointY);
//根据xml内容,得到地图图片数组
for each ( var element:XML in mapXml.imageMap.elements( ) ) {
var words:Array = ("" + element).split( ",");
_map.push(words);
}
//根据xml内容,得到地形数组
for each ( var datement:XML in mapXml.dataMap.elements( ) ) {
var dates:Array = ("" + datement).split( ",");
_mapDate.push(dates);
}
//根据xml内容,得到地图跳转信息
for each ( var changement:XML in mapXml.changeMap.elements( ) ) {
var changeList:Array = new Array();
changeList.push(changement.@name);
if(("" + changement.@coordinate).length > 0){
var thisPointArray:Array = ("" + changement.@coordinate).split( ",");
var thisPoint1:Point = new Point(thisPointArray[0],thisPointArray[1]);
changeList.push(thisPoint1);
}else{
changeList.push(null);
}
_changeList.put("" + changement,changeList);
}
//加载地图
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_loader.load(new URLRequest("images/map/stage.jpg"));
}
最终代码如下:
package com{
import flash.net.URLLoader;
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.geom.Point;
import flash.events.MouseEvent;
public class Map001 extends MovieClip {
//声明载入图片类
private var _loader:Loader = new Loader( );
//声明Bitmap
private var _image:Bitmap;
private var __image:Bitmap;
//装载地图小位图用
private var _mapList:Array= new Array();
private var _mapXmlSrc:String;
//地图图片数组
private var _map:Array= new Array();
//地形数组
private var _mapDate:Array= new Array();
//人物
private var _player:PeopleClass;
//滚屏用变量
private var _mapStartX:int = 0;
private var _mapEndX:int = 0;
private var _mapStartY:int = 0;
private var _mapEndY:int = 0;
//地图显示的起始坐标
public var _mPoint:Point;
//地图最大坐标
public var _maxX:int;
public var _maxY:int;
//人物主角图片
private var _propleImage:String;
//人物初始坐标
public var _peoplePoint:Point;
//地图画板
public var _mapFrame:MovieClip = new MovieClip();
//地图跳转信息
public var _changeList:HashMap = new HashMap();
public var _gameMap:GameStart;
public function Map001(mapXmlSrc:String,gameMap:GameStart) {
addChild(_mapFrame);
_gameMap = gameMap;
//获取地图xml文件
_mapXmlSrc = mapXmlSrc;
var loader:URLLoader = new URLLoader(new URLRequest(_mapXmlSrc));
loader.addEventListener(Event.COMPLETE,initMap);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
public function mouseDownHandler(evet:MouseEvent):void{
//设定光标
stage.focus = this;
}
public function initMap(evet:Event):void{
//得到xml信息
var mapXml:XML = new XML(evet.target.data);
//得到人物图片
_propleImage = mapXml.hero.Image;
//取得地图最大size
_maxX = mapXml.dataMap.@maxX;;
_maxY = mapXml.dataMap.@maxY;
//得到人物坐标信息
var peoplePoint:Point = _gameMap.getPoint();
if(peoplePoint == null){
var strPoint:String = mapXml.hero.Coordinate;
var pointArray:Array = strPoint.split( ",");
_peoplePoint = new Point(pointArray[0],pointArray[1]);
}else{
_peoplePoint = peoplePoint;
}
//计算人物坐标和显示位置
var mPointX:int = 0;
var mPointY:int = 0;
if(_peoplePoint.x > 5){
if(_peoplePoint.x + 5 <= _maxX){
mPointX = _peoplePoint.x - 5;
_peoplePoint.x = 5;
}else{
mPointX = _maxX - 10;
if(mPointX > 0){
_peoplePoint.x = _peoplePoint.x - mPointX;
}
}
}
if(_peoplePoint.y > 5){
if(_peoplePoint.y + 5 <= _maxY){
mPointY = _peoplePoint.y - 5;
_peoplePoint.y = 5;
}else{
mPointY = _maxY - 10;
if(mPointY > 0 ){
_peoplePoint.y = _peoplePoint.y - mPointY;
}
}
}
_mPoint = new Point(mPointX,mPointY);
//根据xml内容,得到地图图片数组
for each ( var element:XML in mapXml.imageMap.elements( ) ) {
var words:Array = ("" + element).split( ",");
_map.push(words);
}
//根据xml内容,得到地形数组
for each ( var datement:XML in mapXml.dataMap.elements( ) ) {
var dates:Array = ("" + datement).split( ",");
_mapDate.push(dates);
}
//根据xml内容,得到地图跳转信息
for each ( var changement:XML in mapXml.changeMap.elements( ) ) {
var changeList:Array = new Array();
changeList.push(changement.@name);
if(("" + changement.@coordinate).length > 0){
var thisPointArray:Array = ("" + changement.@coordinate).split( ",");
var thisPoint1:Point = new Point(thisPointArray[0],thisPointArray[1]);
changeList.push(thisPoint1);
}else{
changeList.push(null);
}
_changeList.put("" + changement,changeList);
}
//加载地图
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_loader.load(new URLRequest("images/map/stage.jpg"));
}
public function onComplete(event:Event):void {
_image = Bitmap(_loader.content);
//将地图文件分解成数组
_mapList = ImageCtrl.divide(_image,10,10);
//加载人物
_player = new PeopleClass(this,_propleImage,2,4,_peoplePoint);
//绘制地图
drowMap();
//设定光标
stage.focus = this;
}
public function drowMap():void{
//地图清空
BaseRemove.removeAllChildren(_mapFrame);
//地图重绘
for(var i:int= _mPoint.y + _mapStartY;i<_mPoint.y + _mapEndY + 10;i++){
for(var j:int=_mPoint.x + _mapStartX;j<_mPoint.x + _mapEndX + 10;j++){
var index:int = _map[j];
var indexX:int = Math.floor(index /10);
var indexY:int = index - indexX*10;
__image = new Bitmap(_mapList[indexX][indexY]);
__image.x = (j - _mPoint.x)*48;
__image.y = (i - _mPoint.y)*48;
_mapFrame.addChild(__image);
}
}
}
//取得地形数组
public function getMapDate():Array{
return _mapDate;
}
//设定偏移
public function setCoordinate(intStartX:int = 0,intEndX:int = 0,intStartY:int = 0,intEndY:int = 0):void{
_mapStartX = intStartX;
_mapEndX = intEndX;
_mapStartY = intStartY;
_mapEndY = intEndY;
}
public function removeSelf():void{
BaseRemove.removeAllChildren(_mapFrame);
}
}
}
最后是PeopleClass.as中,在人物移动中添加跳转判断
//移动控制
private function toMove():void{
//移动达到一个坐标判断
if(_moveCtrl >= 4){
_moveCtrl =0;
//如果滚屏移动,则重绘地图
if(_mapIsRoll){
_mapBack._mapFrame.x = 0;
_mapBack._mapFrame.y = 0;
_mapBack.setCoordinate();
switch (_playDirection) {
case 0 :
_mapBack._mPoint.y -= 1;
break;
case 1 :
_mapBack._mPoint.y += 1;
break;
case 2 :
_mapBack._mPoint.x -= 1;
break;
case 3 :
_mapBack._mPoint.x += 1;
break;
}
_mapBack.drowMap();
}
//检测跳转
if(_mapBack._changeList.getValue(_mapPoint.x + "," + _mapPoint.y) != null){
var changeList:Object = _mapBack._changeList.getValue(_mapPoint.x + "," + _mapPoint.y);
_mapBack._gameMap.setMapName(changeList[0]);
_mapBack._gameMap.setPoint(changeList[1]);
_mapBack.removeSelf();
BaseRemove.removeAllChildren(this);
_mapBack._gameMap.selectMap();
return;
}
}
if(!_isKeyDown){
//键盘弹起状态处理
keyUpCheck();
}else{
//键盘按下状态处理
keyDownCheck();
}
}
最终代码如下:
package com{
import flash.net.URLLoader;
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class PeopleClass extends MovieClip {
//声明time函数
private var _timer:Timer;
//位图数据数组
protected var _bitmapArr:Array;
//播放的顺序 0为上 1为下 2为左 3为右
private var _playDirection:int = 1;
//当前帧显示的位图数据的索引
private var _bitmapIndex:int=0;
//图片列数
private var _row:int;
//图片行数
private var _col:int;
private var _pointer:int;
//加载图片用
private var _loader:Loader;
//读取图片用
private var _image:Bitmap;
//用以显示的bitMap对象
private var _peopleBitmap:Bitmap;
//当前地图类
private var _mapBack:Map001;
//移动步数控制
public var _moveCtrl:int = 0;
//移动步长
public var _moveLong:int = 12;
private var _isKeyDown:Boolean = false;
//人物在画面中坐标
private var _point:Point;
//人物在地图中坐标
private var _mapPoint:Point;
//滚屏控制
private var _mapIsRoll = false;
//构造器 mapBack:当前地图 player:人物图片 row:图片列数 col:图片行数
public function PeopleClass(mapBack:Map001,player:String,row:int,col:int,point:Point = null) {
_mapBack = mapBack;
_row = row;
_col = col;
if(point == null){
_point = new Point(5,5);
_mapPoint = new Point(5,5);
}else{
_point = new Point(point.x*48,point.y*48);
_mapPoint = new Point(_mapBack._mPoint.x + point.x,_mapBack._mPoint.y + point.y);
}
//初始化time;
_timer = new Timer(100);
_timer.addEventListener(TimerEvent.TIMER, timerHandler);
//图片加载对象;
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_loader.load(new URLRequest(player));
//用来添加侦听器,侦听键盘
_mapBack.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_mapBack.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
public function onComplete(event:Event):void {
_image = Bitmap(_loader.content);
//将传入的位图数据拆分成小块,装入bitmapArr
_bitmapArr=ImageCtrl.divide(_image,_row,_col);
//角色动作开始;
_timer.start();
}
//键盘事件,通过方向键更改角色移动方向;
private function keyDownHandler(event:KeyboardEvent):void {
//人物移动不够一个坐标,则返回
if(_moveCtrl > 0){
return;
}
_isKeyDown = true;
switch (event.keyCode) {
case 40 :
_playDirection = 1;
break;
case 38 :
_playDirection = 0;
break;
case 37 :
_playDirection = 2;
break;
case 39 :
_playDirection = 3;
break;
}
}
//键盘事件,
private function keyUpHandler(event:KeyboardEvent):void {
_isKeyDown = false;
}
//定时器运行事件;
private function timerHandler(event:Event):void {
//删除旧的角色动作图像;
if (_peopleBitmap != null) {
_mapBack.removeChild(_peopleBitmap);
}
//显示新的角色动作图像;
_peopleBitmap = new Bitmap(_bitmapArr[_playDirection][_pointer]);
//角色坐标
_peopleBitmap.x = _point.x;
_peopleBitmap.y = _point.y;
_mapBack.addChild(_peopleBitmap);
//角色动作循环处理;
if (_pointer < _row - 1) {
_pointer ++;
} else {
_pointer = 0;
}
//移动控制
toMove();
}
private function keyUpCheck(){
//人物移动达到一个坐标,则停止,否则继续移动
if(_moveCtrl == 0){
_mapIsRoll = false;
return;
}else{
coordinateChange();
}
}
private function keyDownCheck(){
//人物移动达到一个坐标,则进行下一个坐标的判断,否则继续移动
if(_moveCtrl == 0){
switch (_playDirection) {
case 0 :
checkUp();
break;
case 1 :
checkDown();
break;
case 2 :
checkLeft();
break;
case 3 :
checkRight();
break;
}
}else{
coordinateChange();
}
}
//移动执行
private function coordinateChange():void{
//人物移动
if(!_mapIsRoll){
switch (_playDirection) {
case 0 :
_point.y -= _moveLong;
break;
case 1 :
_point.y += _moveLong;
break;
case 2 :
_point.x -= _moveLong;
break;
case 3 :
_point.x += _moveLong;
break;
}
}else{
//滚屏移动
switch (_playDirection) {
case 0 :
_mapBack._mapFrame.y += _moveLong;
break;
case 1 :
_mapBack._mapFrame.y -= _moveLong;
break;
case 2 :
_mapBack._mapFrame.x += _moveLong;
break;
case 3 :
_mapBack._mapFrame.x -= _moveLong;
break;
}
//地图重绘
_mapBack.drowMap();
}
_moveCtrl += 1;
}
//移动控制
private function toMove():void{
//移动达到一个坐标判断
if(_moveCtrl >= 4){
_moveCtrl =0;
//如果滚屏移动,则重绘地图
if(_mapIsRoll){
_mapBack._mapFrame.x = 0;
_mapBack._mapFrame.y = 0;
_mapBack.setCoordinate();
switch (_playDirection) {
case 0 :
_mapBack._mPoint.y -= 1;
break;
case 1 :
_mapBack._mPoint.y += 1;
break;
case 2 :
_mapBack._mPoint.x -= 1;
break;
case 3 :
_mapBack._mPoint.x += 1;
break;
}
_mapBack.drowMap();
}
//检测跳转
if(_mapBack._changeList.getValue(_mapPoint.x + "," + _mapPoint.y) != null){
var changeList:Object = _mapBack._changeList.getValue(_mapPoint.x + "," + _mapPoint.y);
_mapBack._gameMap.setMapName(changeList[0]);
_mapBack._gameMap.setPoint(changeList[1]);
_mapBack.removeSelf();
BaseRemove.removeAllChildren(this);
_mapBack._gameMap.selectMap();
return;
}
}
if(!_isKeyDown){
//键盘弹起状态处理
keyUpCheck();
}else{
//键盘按下状态处理
keyDownCheck();
}
}
//右移判断
private function checkRight():void{
if(checkMap(_mapPoint.x + 1,_mapPoint.y)){
//是否滚屏移动
if(_point.x >= 240 && _mapBack._mPoint.x < _mapBack._maxX - 10){
_mapBack.setCoordinate(0,1);
_mapBack._mapFrame.x -= _moveLong;
_mapBack.drowMap();
_mapIsRoll = true;
}else{;
_mapIsRoll = false;
_point.x += _moveLong;
}
_moveCtrl += 1;
_mapPoint.x += 1
}
}
//左移判断
private function checkLeft():void{
if(checkMap(_mapPoint.x - 1,_mapPoint.y)){
//是否滚屏移动
if(_point.x <= 192 && _mapBack._mPoint.x > 0){
_mapBack.setCoordinate(-1,0);
_mapBack._mapFrame.x += _moveLong;
_mapBack.drowMap();
_mapIsRoll = true;
}else{
_mapIsRoll = false;
_point.x -= _moveLong;
}
_mapPoint.x -= 1;
_moveCtrl += 1;
}
}
//下移判断
private function checkDown():void{
if(checkMap(_mapPoint.x, _mapPoint.y + 1)){
//是否滚屏移动
if(_point.y >= 240 && _mapBack._mPoint.y< _mapBack._maxY - 10){
_mapBack.setCoordinate(0,0,0,1);
_mapBack._mapFrame.y -= _moveLong;
_mapBack.drowMap();
_mapIsRoll = true;
}else{
_mapIsRoll = false;
_point.y += _moveLong;
}
_mapPoint.y += 1;
_moveCtrl += 1;
}
}
//上移判断
private function checkUp():void{
if(checkMap(_mapPoint.x,_mapPoint.y - 1)){
//是否滚屏移动
if(_point.y <= 192 && _mapBack._mPoint.y > 0){
_mapBack.setCoordinate(0,0,-1,0);
_mapBack._mapFrame.y += _moveLong;
_mapBack.drowMap();
_mapIsRoll = true;
}else{
_mapIsRoll = false;
_point.y -= _moveLong;
}
_mapPoint.y -= 1;
_moveCtrl += 1;
}
}
//障碍判断
private function checkMap(mapX:int,mapY:int):Boolean{
var mapDate:Array = _mapBack.getMapDate();
if(mapDate[mapY][mapX] == 0){
return true;
}else{
return false;
}
}
}
}
最后,做一个仿HashMap类,HashMap.as
package com
{
import flash.utils.Dictionary;
public class HashMap
{
private var _keys:Array=null;
private var props:Dictionary=null;
public function HashMap() {
this.clear();
}
public function clear():void {
this.props=new Dictionary ;
this._keys=new Array ;
}
public function containsKey(key:Object):Boolean {
return this.props[key]!=null;
}
public function containsValue(value:Object):Boolean {
var result:Boolean=false;
var len:uint=this.size();
if (len>0) {
for (var i:uint=0; i<len; i++) {
if (this.props[this._keys]==value) {
result = true;
break;
}
}
}
return result;
}
public function getValue(key:Object):Object {
return this.props[key];
}
public function put(key:Object,value:Object):Object {
var result:Object=null;
if (this.containsKey(key)) {
result=this.getValue(key);
this.props[key]=value;
} else {
this.props[key]=value;
this._keys.push(key);
}
return result;
}
public function remove(key:Object):Object {
var result:Object=null;
if (this.containsKey(key)) {
delete this.props[key];
var index:int=this._keys.indexOf(key);
if (index>-1) {
this._keys.splice(index,1);
}
}
return result;
}
public function putAll(map:HashMap):void {
this.clear();
var len:uint=map.size();
if (len>0) {
var arr:Array=map.keys();
for (var i:uint=0; i<len; i++) {
this.put(arr,map.getValue(arr));
}
}
}
public function size():uint {
return this._keys.length;
}
public function isEmpty():Boolean {
return this.size() <1;
}
public function values():Array {
var result:Array=new Array ;
var len:uint=this.size();
if (len>0) {
for (var i:uint=0; i<len; i++) {
result.push(this.props[this._keys]);
}
}
return result;
}
public function keys():Array {
return this._keys;
}
}
}
然后,Ctrl + 回车,看下运行结果
这样,就实现了地图的跳转了,
下次,该让NPC人物出场了,哈
[ 本帖最后由 lufy 于 2009-11-28 15:14 编辑 ]
|
|
|