来自 游手好学 的文章
上周上线的游戏返回数据表明,我的游戏做的比较难,需要加个小地图降低难度,觉得实现小地图的方法还是比较简单的,而且在游戏中比较实用,拿来和大家分享下吧,实现方法其实就是根据缩放比率刷新小地图上人物的X,Y坐标。
实现的效果演示:
[flash]http://www.8ria.com/wp-content/uploads/2009/12/miniMap.swf[/flash]
小地图类,MiniMap.as
- /**
- * 游手好学游戏 – 小地图的实现
- * @author Vincent
- * @website www.8ria.com
- */
- package
- {
- import flash.display.MovieClip;
- import flash.display.Sprite;
- public class MiniMap extends MovieClip
- {
- private var _rate : Number;
- private var _square : Sprite;
- private var _point : Sprite;
- public function MiniMap()
- {
- }
- /**
- * 安装小地图
- * con 小地图的容器
- * bigWidth 大地图的宽度
- * bigHeight 大地图的高度
- * rate 地图缩放比率
- * alp 小地图的透明度
- */
- public function setupMiniMap(con : Object, bigWidth : Number, bigHeight : Number, rate : Number, alp : Number):void
- {
- //画小地图
- _square = new Sprite();
- _square.graphics.beginFill(0×000000);
- _square.graphics.drawRect(0, 0, bigWidth*rate, bigHeight*rate);
- _square.alpha = alp
- con.addChild(_square);
- //画点
- _point = new Sprite();
- _point.graphics.beginFill(0xFF0000);
- _point.graphics.drawCircle(4, 4, 4);
- con.addChild(_point);
- _rate = rate;
- }
- /**
- * 刷新玩家位置
- * px 玩家的X坐标
- * py 玩家的X/Y坐标
- */
- public function updataMiniMap(px : Number, py : Number):void
- {
- _point.x = px * _rate;
- _point.y = py * _rate;
- }
- }
- }
使用小地图类
第一安装MiniMap:
_miniMap = new MiniMap();
_miniMap.setupMiniMap(this,stage.stageWidth,stage.stageHeight,0.25, 0.5);
第二,把_miniMap.updataMiniMap(_player.x,_player.y)放到游戏循环里,就是不断的刷新_player的X,Y坐标
我的文档类如下
- /**
- * 游手好学游戏 – 小地图的实现
- * @author Vincent
- * @website www.8ria.com
- */
- package{
- import flash.display.Sprite;
- import flash.display.MovieClip;
- import flash.events.Event;
- public class IndexDoc extends Sprite
- {
- private var _miniMap : MiniMap;
- private var _player : MovieClip;
- public function IndexDoc():void
- {
- initUI();
- initEvent();
- }
- private function initUI():void
- {
- _miniMap = new MiniMap();
- //安装小地图
- _miniMap.setupMiniMap(this,stage.stageWidth,stage.stageHeight,0.25, 0.5);
- _player = fly_mc;
- }
- private function initEvent():void
- {
- stage.addEventListener(Event.ENTER_FRAME, gameLoop);
- }
- private function gameLoop(e:Event):void
- {
- //刷新小地图
- _miniMap.updataMiniMap(_player.x,_player.y);
- flyHandler();
- }
- private function flyHandler():void
- {
- var dx : Number = mouseX – _player.x;
- var dy : Number = mouseY – _player.y;
- _player.x += dx/10;
- _player.y += dy/10;
- var angle : Number = Math.atan2(dy, dx) * 180 / Math.PI;
- _player.rotation = angle + 90;
- }
- }
- }